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.

67 Comments

This would work well as a plugin, so all you have to is drop it into

/path/to/wordpress/wp-content/plugins/

update: I removed the link to jackson’s earlier version of the code. Use the link in the post above. –justin

For some reason, I don’t get anything at your link, Jackson. It could be an issue locally here, but just wanted to check.

jackson, change the file extension to .phps or .txt—otherwise php parses the file and returns a blank page.

As seen on Justin’s blog, create a more useful monthly archive calendar for your WordPress sidebar.

very cool – i may use it.

Now, if we can get you to release your theme, I’d be very happy. Sweet looking site, btw

for everyone else who’s following along, I’ve fixed jackson’s link so it now points to a .phps file. jackson, i was curious to see what you’d done, and as it turns out, all you had to do was add the required php comments for the plugin. duh!

i pretty much rewrote the post based on jackson’s plugin idea. hope that makes it a little easier for people to use.

redjade, hmm, i’m not sure my theme is ready for primetime, it’s pretty much a hacked together work in progress. if you’d like to know how i did this or that, do feel free to ask or email.

Looks like your source has odd character encoding…when I grab it and open it in a text editor, I get code for non-breaking spaces and such.

Check it out and tell me if I’m nuts.

jason, the phps file is an html file that highlights the php source. you have to copy the text and paste it into a new file, don’t do a right click > save link as, or you’ll download the colorized html version.

This was easy to install and looks so much better than a list with 50 entries.

hmmm something went wrong:
http://allotherplaces.org

ideas?

thx

here’s what the error is:

Fatal error: Call to undefined function: monthchunks() in /home/Shell/…. etc

ideas?

Don’t know if I’ll ever use this plugin with my current layout, but I hope to in the future — MonthChunks takes your archives list and outputs it into a much cleaner, organized, shorter space.

redjade, did you activate the plugin? (guess i forgot to mention that step above)

Odd… while I do have posts from 2005, the plugin starts at 2004 and goes back from there. How do I get the 2005 months to show?

raphaële

hello,
very cool plugin. thank u.
i’m using WordPress 1.5, at the begginning, the links to my archives where wrong so in
in the line 51 of the monchuck file, i use get_month_link($year, $month) to get well formatted url. this function can be find in template-functions-link.php file
it makes the line become:
print "<a href='" . get_month_link($year, $month) . "'>" . $month . "</a > ";

trying try again :-)

This time I noticed that when I activate teh plugin i get theis mess at the top of the wordpress page:

‘Parse error: parse error in /[etc not post it here]/www.allotherplaces.org/wp-content/plugins/monthchunks.php on line 16’

there must be something obvious I’m not doing right.

bishless: very strange. are your 2005 posts stored in a different table or a different database or something? the first query in the plugin should get a list of all the years in which there are posts. why 2005 isn’t at the top, i don’t know.

unless! for whatever reason in 2005 wordpress started leaving the post_date field null and put the 2005 dates in the post_date_gmt field? are you able to verify the values in the post_date and post_date_gmt field for your 2005 posts (with phpmyadmin or the mysql commandline client)?

raphaële, way cool. i’ve incorporated your change into plugin (making it version 1.1) and made a few other cosmetic changes.

redjade, since line 16 is just the global $wpdb; in v1.0, I’m not sure what’s going on. did you make sure that when you copied the file into monthchunks.php that there are no spaces before the <?php and no spaces after the ?>

Justin: Wow. I didn’t realize that the post date was/could be stored in two different fields. I did a quick check and it appears that my WP 1.5 installation is writing date information to both of the fields you identified… even the 2005 posts.

i’m patient but still stumped.

bishless, are you comfortable using the mysql command line client?

if so, you might want to try running the following query and see what the results are. see if 2005 comes up? if so, there could be a bug in the $wpdb object code.


SELECT DATE_FORMAT(post_date, '%Y') as post_year
FROM wp_posts
GROUP BY post_year
ORDER BY post_year DESC

OR, i just noticed I hardcoded in the wp_posts table name… but wordpress allows you to customize the table name prefixes (e.g. wp)–when you checked the date fields, were you looking at a table named wp_posts? or perhaps something else?

i’ll update the code to use variables for the table names…

Golfy

OH MY! I want to cry…
This plugin is so awesome! It easy to install and does exactly what I have *dreamed* of :)
Justin (and Jackson), you’re the best thing given to mankind next to Residence Evil 4 :) Thank you Thank You ~

I’ve sucessfully converted the long and ugly MONTHLY ARCHIVES into MONTHCHUNKS style and it looks so pretty.

bishless, i updated the plugin so that it refers to the variable table name $wpdb->posts instead of hardcoding wp_posts as the table name. see if that helps at all.

golfy, glad to hear that this hit the spot for you. you’re very welcome. p.s. what’s your url?

HELLO :)
Hey if you don’t mind, let me SHOW OFF my lovely MONTHCHUNKS! Here’s my silly little blog http://www.bloggie.com/ ;)

ah-ha, so golfy = spygirl. excellent.

FIXED!!! 1.2 fixes the problem. You were right about the customized table names. Turns out that I’d left old data in the db under the default table names… just upgraded w/ bishxx-posts and such.

Thanks for the fix! It works great!

bishless, great! i was really stumped and didn’t expect that to fix it for you. what a lucky hunch.

So is there any way that someone still on Blogger (waves hand in the air with earnest and pleading look on face) could wangle the original javascript to turn the ‘standard archive link into something a little more compact’? I assume this would be code that goes into my custom template in place of the month-by-month links I’ve got there now? Short of a real calendar, the less vertical space used up by my archive links list, the happier a camper I am. Many thanks, I hope!

leeeny, the short answer is yes. actually before i moved to wordpress i had some javascript that produced a very similar output. i’ll see if i can dig it up for you.

That would be wonderful, Justin. If you don’t want to post it here on your own site, you can flag me down with an email, there’s a link on my own blog, which you can get to by clicking my name above. Many thanks, hope you can find time for this soonish.

leeeny, here you go. check out monthchunks for blogger

chemist

Hi,
The question about monthchunks wants to ask …
can let the month of the article be shown too, but has not linked ?
Example:
2005,
1 2 3 4 5 6 7 8 9 10 (11) 12

Thanks.

bill

Is there a way to modify the code such that if a users clicks on Feb for 2004, the monthchunk display would provide feedback of the month their viewing (as in):

2004 1 2 3 4 … ?

chemist and bill, sorry it took so long, but I’ve updated monthchunks to do exactly what you requested. Now when you’re viewing a monthly archive page, the monthchunks link for that month is delinked and emboldened.

Excellent plugin.
Would anyone be able to tell me what to change to reverse the display of the years? So 2005 is at the top and 2004 etc below. – Thanks :)

Tancred, there isn’t an option to do this, though there probably should be. For now, just change line 71 (v2.0) from

ORDER BY post_year");

to

ORDER BY post_year DESC");

Sweet :) Worked like a charm

Thanks a lot

Hi – I’ve loved monthchunks for a while, but just upgraded to wp 2.0 and (perhaps coincidentally) now have an odd issue with monthchunks — in addition to all the regular M/Y combinations it now lists a year 0000 and a month 0. I wonder if it will keep adding in February, or if the current month will be the new month 0? At any rate, I have no idea what might have caused this in the wp 2.0 upgrade, or anything else, and wondered if anybody else had experienced it?

… and unrelatedly, I was wondering if you had ever thought of changing the month displays from 1 2 3 … to J F M A M J J A S O N D ? (or know if an easy way to do it …)

Laura, well that’s ugly, isn’t it. I haven’t had time to do the WP 2.0 upgrade, so I’m not sure why it would happen, and it’s stranger still that selecting that month is tantamount to selecting the current month. I would have guessed that maybe WP inserted some default post with a default date of 00/00/00 00:00:00 or something, but nothing like that turns up. How very odd. I’ll will try to look into it later tonight.

Re: letters instead of numbers for month links, that’s an interesting idea, not one I’d thought of. Sounds like it’s time to give monthchunks some configurable options.

Laura and Tancred, I just revised the plugin (to v2.1) so that you can now change the year order and output the first letter of each month if you want.

I also made some minor code improvements and added a check to prevent a 0000 year from showing up.

It there a way to get month chunks to not put the bullet point before the years. I noticed your output description show the bullet point, but your actual use of month chunks on this blog does not. I really like it, but the bullet point just does not fit with my blog.

Doug, looking at the html source of your site, it looks like maybe you’ve modified the monthchunks plugin? The easiest way to get rid of the bullets is using CSS. In the appropriate WP template (index.php or sidebar.php), you’d do something like this:

<ul id="monthchunks">
<?php monthchunks(); ?>
</ul>

and then in your theme’s style.css file you’d add this to the bottom:

#monthchunks {
    list-style-type:none;
    margin:0;
    padding:0;
}

Hope this helps.

Thanks, If I modified anything it was completely accidential. I don’t know enought to even be dangerous, or at least I though I did not.

Doug, that wasn’t a criticism, more just trying to determine what level you wanted to hack this plugin. When I look at the html source generated by your monthchunks plugin, I see the following invalid html:

<h2><li>2006<br /></h2>

Notice the <h2> tags are incorrectly nested around the <li> start tag. This is what’s causing a bottom-border to appear beneath each year. Since the plugin code itself doesn’t use <h2> tags, I’m assuming you adding them by modifying line 124 (of v2.1) of the plugin:

print "<li><strong>$year</strong><br />\\n";

If you want <h2> tags to appear around the years correctly, that line should look like this:

print "<li><h2>$year</h2><br />\\n";

Hope this helps.

Thank you very much. I forgot that I had tried to make some changes to get rid of the bullets and not changed it back. Now it looks like I want it to look.

Doug, I’m glad that worked out for you (I had a hunch what was going on…). Looks good.

[…] monthchunks Web El archivo mensual de un blog es tan necesario como molesto. En mi opinión pocas cosas son tan antiestéticas en una bitácora como una barra lateral llena de meses y años. Para solucionarlo, este plugin. Fijáos en mi página de inicio y veréis el resultado. La instalación no es complicada si se domina la lengua de Sherlock Holmes.  […]

[…] monthchunks-2.1.zipをダウンロードして有効化。 […]

[…] monthchunks Web El archivo mensual de un blog es tan necesario como molesto. En mi opinión pocas cosas son tan antiestéticas en una bitácora como una barra lateral llena de meses y años. Para solucionarlo, este plugin. Fijáos en mi página de inicio y veréis el resultado. La instalación no es complicada si se domina la lengua de Sherlock Holmes. […]

[…] monthchunks-2.1.zipをダウンロードして有効化。 sidebar.php <?php wp_get_archives(’type=monthly’); ?> の代わりに、<?php monthchunks(); ?>を入力する。 […]

[…] 3:55 PM Addition: Justin came through for me again.  His monthchunks plugin pulled my archives up into a much neater and compact format.  Thanks Justin! Filed under: Just Life   |     […]

[…] monthchunks-2.1.zip をダウンロードする […]

Thank you for nice plugin!
The archive list of my blog became concise.
Thanks…

[…] Just installed Justin’s Monthchunks. Sweet plugin. If you like it, be sure to send him some PayPal love like I did. […]

Will it work for WordPress 2.1?

Xavi, I don’t know, I’ll test with the release candidate when I get a chance

Thank you for the plugin! Love it!

superfuge

Great plugin, but does anybody know how to get the whole month instead of the first letter only in other language that is not English?Thank you

superfuge, in that case, I’d recommend configuring the plugin to output the month number instead of the first letter (which happens to be the default behavior).

It is fails in the judgment of “month in bold” when the archive exceeds 128 months.

hide10, sorry about that. sounds like something changed with the get_the_time() function in WordPress 2.7. I haven’t had a change to upgrade, so I really haven’t looked into it just yet. Hope to soon.

Care to Comment?

Or if you'd prefer to get in touch privately, please send me an email.

Name

Email (optional)

Blog (optional)