简体   繁体   English

在Wordpress中按日期对帖子进行分组

[英]Group posts by date in Wordpress

I need to write a WordPress query to accomplish the following - I am working on a site which has 40-50 posts daily - I want to show the posts "grouped" by date. 我需要编写WordPress查询来完成以下任务-我正在一个每天有40至50个帖子的网站上工作-我想按日期显示“分组”的帖子。

eg 例如

20 March 2012
 post 1
 post 2
 post 3

19 March 2012
 post 4
 post 5
 post 6

Should I use more than 1 query to accomplish this, is it possible to do this without having to write custom SQL query. 如果我使用多个查询来完成此操作,是否有可能这样做而不必编写自定义SQL查询。

I want the posts grouped. 我希望帖子分组。

Yes it is possible: 对的,这是可能的:

$args = array('posts_per_page' => -1, 'orderby' => 'date' );

$myQuery = new WP_Query($args);

$date = '';

if ( $myQuery->have_posts() ) : while ( $myQuery->have_posts() ) : $myQuery->the_post();

if ( $date != get_the_date() ) {
    echo $date;
    echo '<hr />';
    $date = get_the_date();
}

the_title(); // or whatever you want here.
echo '<br />';

endwhile; endif;
wp_reset_postdata();

More info on the query here: http://codex.wordpress.org/Class_Reference/WP_Query 有关查询的更多信息,请访问: http : //codex.wordpress.org/Class_Reference/WP_Query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM