WordPress: List All Posts and Group Them by Month & Year

眉间皱痕 提交于 2019-12-11 04:56:14

问题


When I need to list all posts in 2011 grouped by month, I'd do something like this in WordPress (pretty straightforward as explained here):

query_posts('monthnum=12&year=2011');
while ( have_posts() ) : the_post();
  echo '<li>';
  the_title();
  echo '</li>';
endwhile; 

Now, how to list all posts and group them by month and year, without knowing how far I should go back? That is, I don't know which year was the oldest post written in. Technically, I could try to do monthnum=12&year=2010, monthnum=12&year=2009, and so on; but I feel that there must be a better way.


回答1:


One solution could be to loop through all years:

 for($i=0;$i<10;$i++){
     $y=2011-$id;
     query_posts("monthnum=12&year=$y");
     while ( have_posts() ) : the_post();
        echo '<li>';
        the_title();
        echo '</li>';
     endwhile; 
 }

This may not be an elegant solution, but use it when there is no better one.



来源:https://stackoverflow.com/questions/8503001/wordpress-list-all-posts-and-group-them-by-month-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!