Nested Year/Month Archive in WordPress

This is a feature that I use quite often when building a custom theme.

To sort the archive by year and then month, similar to the one I added on Fellow Fellow, just add the following code inside the loop of your archive page template.

[php] <?php
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date)
FROM $wpdb->posts WHERE post_status = ‘publish’
AND post_type = ‘post’ ORDER BY post_date DESC");

foreach($years as $year):
<h4><?php echo $year; ?></h4>
<ul class="archive-sub-menu">
$months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date)
FROM $wpdb->posts WHERE post_status = ‘publish’ AND post_type = ‘post’
AND YEAR(post_date) = ‘".$year."’ ORDER BY post_date DESC");

foreach($months as $month):
<li><a href="<?php echo get_month_link($year, $month); ?>">
<?php echo date(‘F’, mktime(0, 0, 0, $month));?></a>
<?php endforeach;?>
<?php endforeach; ?>