WordPress – Archive List Current Year by Months and Historical Years

Just thought I would share my *a-ha* moment. Some of you might just call it an *duh* moment.

So, I needed to create an archive list for the sidebar of a new blog. I wanted the following format:

Jul 2011 (#)
Jun 2011 (#)
May 2011 (#)
Apr 2011 (#)
Mar 2011 (#)
Feb 2011 (#)
Jan 2011 (#)
2010 (#)
2009 (#)
2008 (#)

Easy to do one or the other with the built in WP function but not necessarily both – at least that is what I first thought.

My search found a combo of WP function and mySQL data call.

<ul>
	<li>
<ul> <!--?php wp_get_archives('type=monthly&#038;limit='.date('m').'&#038;show_post_count=1'); ?--></ul>
</li>
<!--?php wp_get_archives('type=yearly&#038;limit=1'); ?-->

<!--?php $years = $wpdb--->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC");
unset($years['0']);
foreach($years as $year) :
?&gt;
	<li><a title="&lt;?php echo $year; ?&gt;" href="&lt;?php echo get_year_link($year); ?&gt;"><!--?php echo $year; ?--></a></li>
<!--?php endforeach; ?--></ul>

Fine but it didn’t show my post count for the years. So, tried to find a solution but that required more database stuff blah blah blah.

Then. It hit me.

Do it the simple way stupid.

Just do the following:

In the sidebar PHP or where you want the list.

<ul class="monthsList"> <!--?php wp_get_archives('type=monthly&#038;limit='.date('m').'&#038;show_post_count=1'); ?--></ul>
<ul class="yearsList"> <!--?php wp_get_archives('type=yearly&#038;show_post_count=1'); ?--></ul>

Then update your style.css

ul.yearsList, ul.monthsList { list-style: none; margin-top: 0; margin-bottom: 0; }
ul.yearsList li:first-child { display: none; }