About eight years ago I wrote a WordPress Wednesday post about the great Alistair Lattimore plugin Kottke Style Archives. Incredibly his plugin is still available and still works as a pretty good solution to compactly display a WordPress archives page containing thousands of links.
My only issues with this plugin — and it’s a minor one — is that if you go an entire month without posting anything, you’ll have a glaring hole in your archives list. It’s not a tragedy or anything, but it does kill the beautiful symmetry the plugin is supposed to achieve.
With a little bit of hacking, you can fix this. Below is the somewhat modified version of the core function in his plugin as I use it here on my site. You can drop this into your theme’s functions.php file or use it as a guide to hacking Alistair’s plugin on your own.
/*-----------------------------------------------------
dvg_theme_archives()
-----------------------------------------------------*/
if ( !function_exists( 'dvg_theme_archives' ) ) {
function dvg_theme_archives( $month_separator = ' ' ){
global $wpdb;
$output = '';
$year = '';
$m = 0;
$sql = "
SELECT DATE_FORMAT(post_date, '%Y') AS Year,
DATE_FORMAT(post_date, '%m') AS Month
FROM {$wpdb->posts}
WHERE DATE_FORMAT(post_date, '%Y') <> '0000'
AND post_status = 'publish'
AND post_type = 'post'
GROUP BY DATE_FORMAT(post_date, '%Y'), DATE_FORMAT(post_date, '%m')
ORDER BY DATE_FORMAT(post_date, '%Y') DESC, DATE_FORMAT(post_date, '%m') ASC;
";
$months = $wpdb->get_results($sql);
if ( !empty( $months ) ){
foreach ( $months as $month ){
// Add in the year heading
if ( $year != '' && $year != $month->Year ){
if ( $m != 12 && ( $year != date('Y') ) ){
for( $i = $m + 1; $i <= 12; $i++ ){
$output .= $month_separator.date( 'M', mktime( 0, 0, 0, $i, 1, $year ) );
}
}
}
if ( ( $year == '' ) || ( $year != $month->Year ) ){
if ( strlen( $output ) ){
$output .= "<br>\n<strong><a href=\"" . home_url( '/' ) . $month->Year . "\">" . $month->Year . "</a>:</strong> ";
if ( $month->Month > 1 ){
for( $i = 1; $i < $month->Month; $i++ ){
$output .= date( 'M', mktime( 0, 0, 0, $i, 1, $month->Year ) ).$month_separator;
}
}
}
else{
// this is the most recent year to be displayed
$output .= "\n<strong><a href=\"" . home_url( '/' ) . $month->Year . "\">" . $month->Year . "</a>:</strong> ";
}
}
// Add in the monthly archive links
if ( $year == $month->Year ) $output .= $month_separator;
if ( $year == $month->Year && ( $m != ( $month->Month + 1 ) ) ){
for( $i = $m + 1; $i < $month->Month; $i++ ){
$output .= date( 'M', mktime( 0, 0, 0, $i, 1, $month->Year ) ).$month_separator;
}
}
$output .= '<a href="' . get_month_link( $month->Year, $month->Month ) . '">' . date( 'M', mktime( 0, 0, 0, $month->Month, 1, $month->Year ) ) . '</a>';
$year = $month->Year;
$m = $month->Month;
}
}
else{
$output = "<p>None available</p>\n";
}
echo $output;
}
}
