在Wordpress中按日期对帖子进行分组 [英] Group posts by date in Wordpress

查看:66
本文介绍了在Wordpress中按日期对帖子进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写WordPress查询来完成以下任务-我正在一个每天有40至50个帖子的网站上工作-我想按日期显示分组"的帖子.

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.

例如

20 March 2012
 post 1
 post 2
 post 3

19 March 2012
 post 4
 post 5
 post 6

我应该使用多个查询来完成此操作,是否有可能这样做而不必编写自定义SQL查询.

Should I use more than 1 query to accomplish this, is it possible to do this without having to write custom SQL query.

我希望帖子分组.

推荐答案

是的,有可能:

$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();

有关查询的更多信息,请参见: http://codex.wordpress.org/Class_Reference/WP_Query

More info on the query here: http://codex.wordpress.org/Class_Reference/WP_Query

这篇关于在Wordpress中按日期对帖子进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆