Charles Stricklin, WordPress Podcast guru, had an interesting idea for arranging monthly WordPress archives, which is basically to make an archives page like this:
| 2005 | Feb | Apr | Jun | Jul | Aug | Sep | Oct | Nov | Dec | |||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2006 | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | |
| 2007 | Jan | |||||||||||
| 2008 | Jan | Apr | Jun | Oct | 
Here’s how to do it just using the WordPress API (instead of direct database queries):
<?php
if ( ! $calendar = wp_cache_get('year_month_archive', 'archive') ) {
     $calendar = array();
     $year = date('Y');
     $mo_qry = new WP_Query();
     $yr_qry = new WP_Query();
     do {
          $yr_qry->query(array('year' => $year, 'showposts' => 1));
          if ( $yr_qry->have_posts() ) {
               for ( $m = 1; $m <= 12; $m++ ) {
                    $mo_qry->query(array('year' => $year, 'monthnum' => $m, 'showposts' => 1));
                    if ( $mo_qry->have_posts() ) {
                         $calendar[$year][$m] = array(date('M', mktime(1,1,1, $m, 1, $year)), get_month_link($year, $m));
                    } else {
                         $calendar[$year][$m] = array('','');
                    }
               }
          }
          $year = intval($year - 1);
     } while ( $yr_qry->have_posts() );
     ksort($calendar);
     wp_cache_set('year_month_archive', $calendar, 'archive');
}
?>
<table>
<?php foreach( $calendar as $year => $m ) : ?>
     <tr><th><?php echo $year; ?></th>
     <?php foreach( $m as $data ) : ?>
          <td><a href="<?php echo $data[1]; ?>"><?php echo $data[0]; ?></a></td>     
     <?php endforeach; ?>
     </tr>
<?php endforeach; ?>
</table>
				
3 Comments
That’s a real cool way to archive the archive’s.
This is great. Thanks for the code.
Oh, awesome. You really helped me out here. Thank you!