在帖子页面中仅显示Wordpress中的最近2年帖子

时间:2017-03-31 13:11:25

标签: wordpress

我已经为名为Blog的帖子创建了一个页面,在阅读设置中我选择了博客页面作为帖子页面。现在我想要的是我想要仅显示最近两年的帖子,而不是全部。我怎样才能做到这一点?

提前致谢。

1 个答案:

答案 0 :(得分:0)

以下是查询的基本代码。

<?php
$args = array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'order'         => 'DESC',
    'orderby'       => 'date',
    'date_query' => array(
        array(
            'column' => 'post_date_gmt',
            'before' => '2 year ago',
        ),
    ),
);
$query = new WP_Query( $args );
if($query->have_posts()): while($query->have_posts()): $query->the_post();   
    /*
    * Your HTML styles to display the post
    */
    ?>
    <h1><?php the_title(); ?></h1>
    <p><?php the_content(); ?></p>
<?php
endwhile;
wp_reset_postdata();
endif;
?>

另外,请参阅以下文档以获取更多相关信息。

Query in WP Date

希望它适合你

由于

相关问题