Monthchunks plugin for WordPress

Inspiration

WordPress logoI got the following comment on a previous post:

I love your blog design a lot. I especially *LOVE* your MONTHCHUNKS archives. (It’s tiny, clean and stylish!) Is it possible to post a tutorial on how to achieve that sort of layout? I really really wish to add the same MONTHCHUNKS to my blog :D

Sure, no problem. Back when I used Blogger to publish my blog, I wrote some JavaScript to transform Blogger’s verbose archive link list into something a little more compact. But with WordPress, I was able to write a little PHP code to get the job done on the server. Thanks to Jackson for initially suggesting I turn my Monthchunks function into my first WordPress plugin.

Output

The Monthchunks plugin outputs the links to your archives as list items by year, with a link to each month by number:

When you’re viewing a monthly archive page, the number for that month will be bold and not linked. Like the default behavior of wp_get_archives(), Monthchunks only outputs list items <li>...</li> for each year. When you call the function, make sure that it’s wrapped in <ul> or <ol> tags.

Instructions

  1. Download Monthchunks v2.3 from the WordPress.org Plugin Directory (or from me here)
  2. Unzip the file and upload the folder monthchunks to your /wp-content/plugins directory
  3. Activate the plugin
  4. Edit the theme template file sidebar.php in your theme’s directory: /wp-content/themes/name-of-theme
  5. Look for this snippet of code:
    <ul>
        <?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>
    </ul>

    and change it to this:

    <ul>
        <?php monthchunks(); ?>
    </ul>

Options

The monthchunks() function can take two optional parameters in between the parentheses.

<?php monthchunks(year_order, month_format); ?>
  1. year_order is a string ("descending" or "ascending") that determines whether the years are displayed in ascending (e.g. 2001, 2002, 2003…) or descending (e.g. 2003, 2002, 2001…) order. The default is descending.
  2. month_format is a string ("numeric" or "alpha") that determines whether the month links are printed out as numbers (e.g. 1 2 3 4…) or letters (J F M A…). The default is numeric.

If you wanted to sort the years in ascending order with letters for each month, you’d do this:

<ul>
    <?php monthchunks("ascending", "alpha"); ?>
</ul>

Update: Monthchunks was included in the Japanese book, WordPress ???????? (Standard Guidebook), and on its accompanying CD. Check it out!

Questions, comments, and suggestions are always welcome. If you’re interested in contributing to the code behind Monthchunks, it’s hosted on GitHub.

Feel free to if you found this useful.