WordPress:列出所有帖子并按月分组他们&年

时间:2011-12-14 10:33:54

标签: wordpress wordpress-plugin

当我需要列出2011年按月分组的所有帖子时,我会在WordPress中执行类似的操作(非常简单,如here所述):

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

现在,如何列出 所有帖子 并按月分组,不知道我应该回去多远?也就是说,我不知道哪一年是写成的最早的帖子。从技术上讲,我可以尝试monthnum=12&year=2010monthnum=12&year=2009,等等;但我觉得必须有更好的方法。

1 个答案:

答案 0 :(得分:0)

一个解决方案可能是遍历所有年份:

 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; 
 }

这可能不是一个优雅的解决方案,但在没有更好的解决方案时使用它。