WordPress: List all posts and groups by month and year

When I need to list all the posts in 2011, grouped by month, I would do something similar in WordPress (quite simply, as described here ):

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

Now, how to list all the messages and group them by months and years, not knowing how far I have to go back? That is, I do not know in which year the oldest post was set. Technically, I could try to make monthnum=12&year=2010 , monthnum=12&year=2009 , etc .; but I feel that there must be a better way.

+1
source share
1 answer

One solution could be a cycle throughout the 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; } 

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

0
source

Source: https://habr.com/ru/post/1386246/


All Articles