Wordpress - отображать сообщения новее 30 дней - PullRequest
1 голос
/ 26 января 2012

Я схожу с ума, пытаясь понять, почему у меня так сложно заставить WordPress показывать посты не более 30 дней, и я действительно могу использовать второй набор глаз. Я использую следующий код, но на моем сайте ничего не отображается.

<?php
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}

add_filter( 'posts_where', 'filter_where' );
$the_query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
?>


<?php if ($the_query->have_posts()) : ?>

    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>


        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
            <h2 class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
            <div class="meta">
             <span class="date">
                <strong><?php the_time('d'); ?></strong>
                <strong><?php the_time('M'); ?></strong>
             </span>


            </div>

            <div class="entry">
            <?php if ( function_exists( 'get_the_image' ) ) {
        get_the_image( array( 'custom_key' => array( 'post_thumbnail' ), 'default_size' => 'full', 'image_class' => 'alignleft', 'width' => '170', 'height' => '155' ) ); }
        ?>

                <?php the_content('Read More'); ?>
            </div>


        </div>

    <?php endwhile; ?>

    <div class="navigation">
    <?php
        include('includes/wp-pagenavi.php');
        if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
    ?>
    </div>

<?php else : ?>

    <h2 class="center">Not Found</h2>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>
    <?php get_search_form(); ?>

<?php endif; ?>

Ответы [ 2 ]

7 голосов
/ 21 сентября 2012
<?php
  function filter_where($where = '') {
    //posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
  }
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>

Это работает

1 голос
/ 26 января 2012

То, как вы делаете, выглядит хорошо.Попробуйте ввести posts_per_page => -1 в качестве аргумента.

У меня работает следующее:

function filter_where( $where = '' ) {
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
}

add_filter( 'posts_where', 'filter_where' );
$args = array(
    'posts_per_page' => -1,
);
$the_query = new WP_Query($args);
remove_filter( 'posts_where', 'filter_where' );

while ($the_query->have_posts()) {
    $the_query->the_post();
    // do stuff
}

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...