monthchunks plugin for WordPress
The latest version is 2.1, updated January 5, 2006
Inspiration
From a 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 achive that sort of layout? I really really wish to add the same MONTHCHUNKS to my blog :D –golfy
Sure golfy, no problem. Back when I used Blogger to generate Justinsomnia, I had to write some JavaScript to transform Blogger’s standard archive link list into something a little more compact. But with WordPress, I was able to write a little code to get the job done in PHP on the server.
Thanks to Jackson for initially suggesting I turn my monthchunks function into a fully-fledged 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 (new in v2.0).
Note: Like the default behavior of wp_get_archives(), monthchunks() only outputs list items <li>...</li> for each year (new in v2.0). When you call the function, make sure that it’s wrapped in <ul> or <ol> tags, e.g.
<ul> <?php monthchunks(); ?> </ul>
Instructions
- Download and unzip monthchunks-2.1.zip (v2.1)
- Upload the file monthchunks.php to your plugins directory:
/path/to/wordpress/wp-content/plugins - Activate the plugin (don’t forget!)
- Edit the theme template file sidebar.php in your theme’s directory:
/path/to/wordpress/wp-content/themes/name-of-theme - In the default WordPress theme, you can replace the function
wp_get_archives('type=monthly');withmonthchunks();by changing this snippet of code:<li><h2>Archives</h2> <ul> <?php wp_get_archives('type=monthly'); ?> </ul> </li>to this:
<li><h2>Archives</h2> <ul> <?php monthchunks(); ?> </ul> </li>
Options
The monthchunks function can take two optional parameters in between the parentheses.
<?php monthchunks(year_order, month_format); ?>
- year_order is a string (
"ascending"or"descending") 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 ascending. - 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 descending order with letters for each month, you’d do this:
<li><h2>Archives</h2>
<ul>
<?php monthchunks("descending", "alpha"); ?>
</ul>
</li>
As always, let me know if you have any questions or problems. Enjoy!
See monthchunks for blogger for a way to create similar output using JavaScript.
Update: Monthchunks was included in the Japanese book, WordPress 標準ガイドブック (Standard Guidebook), and on its accompanying CD. Check it out!


comments: 67 so far...
jackson
This would work well as a plugin, so all you have to is drop it into
update: I removed the link to jackson’s earlier version of the code. Use the link in the post above. –justin
Jason
For some reason, I don’t get anything at your link, Jackson. It could be an issue locally here, but just wanted to check.
justin
jackson, change the file extension to .phps or .txt—otherwise php parses the file and returns a blank page.
Weblog Tools Collection
As seen on Justin’s blog, create a more useful monthly archive calendar for your WordPress sidebar.
redjade
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
justin
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!
justin
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.
Jason
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.
justin
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.
anti’s blog
This was easy to install and looks so much better than a list with 50 entries.
redjade
hmmm something went wrong:
http://allotherplaces.org
ideas?
thx
redjade
here’s what the error is:
Fatal error: Call to undefined function: monthchunks() in /home/Shell/…. etc
ideas?
clair's universe
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.
justin
redjade, did you activate the plugin? (guess i forgot to mention that step above)
bishless
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 > ";redjade
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.
justin
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)?
justin
raphaële, way cool. i’ve incorporated your change into plugin (making it version 1.1) and made a few other cosmetic changes.
justin
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<?phpand no spaces after the?>bishless
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.
justin
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_poststable 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 namedwp_posts? or perhaps something else?i’ll update the code to use variables for the table names…
Wordpress Cookbook
The solution
http://justinsomnia.org/2005/04/monthchunks-howto/
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 ~
spygirl
I’ve sucessfully converted the long and ugly MONTHLY ARCHIVES into MONTHCHUNKS style and it looks so pretty.
justin
bishless, i updated the plugin so that it refers to the variable table name
$wpdb->postsinstead of hardcodingwp_postsas 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?
Golfy
HELLO :)
Hey if you don’t mind, let me SHOW OFF my lovely MONTHCHUNKS! Here’s my silly little blog http://www.bloggie.com/ ;)
justin
ah-ha, so golfy = spygirl. excellent.
bishless
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!
justin
bishless, great! i was really stumped and didn’t expect that to fix it for you. what a lucky hunch.
Leeeny
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!
justin
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.
Leeeny
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.
justin
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 … ?
justin
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.
Tancred
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 :)
justin
Tancred, there isn’t an option to do this, though there probably should be. For now, just change line 71 (v2.0) from
to
Tancred
Sweet :) Worked like a charm
Thanks a lot
laura quilter
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 …)
justin
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.
justin
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.
Doug
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.
justin
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:
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.
Doug
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.
justin
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:
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:If you want
<h2>tags to appear around the years correctly, that line should look like this:Hope this helps.
Doug
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.
justin
Doug, I’m glad that worked out for you (I had a hunch what was going on…). Looks good.
Moonshadow » Los plugins de mi WordPress
[...] 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. [...]
404 » Blog Archive » 月別アーカイブリストを簡潔に monthchunks
[...] monthchunks-2.1.zipをダウンロードして有効化。 [...]
Mis Algoritmos » Plugins para Wordpress
[...] 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. [...]
blog.detlog.org » Archive » New Theme for This Site
[...] monthchunks [...]
blog.detlog.org » Archive » WordPress Standard Guidebook
[...] Justin Watt (monthchunks) [...]
404 » Blog Archive » 月別アーカイブリストを簡潔に monthchunks
[...] monthchunks-2.1.zipをダウンロードして有効化。 sidebar.php <?php wp_get_archives(’type=monthly’); ?> の代わりに、<?php monthchunks(); ?>を入力する。 [...]
Because I’m White and Nerdy at glossyveneer.com
[...] 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 | [...]
Quiet* » 月別アーカイブをコンパクトに
[...] monthchunks-2.1.zip をダウンロードする [...]
mofu
Thank you for nice plugin!
The archive list of my blog became concise.
Thanks…
BeatGrip.com » monthchunks plugin for Wordpress
[...] Justinsomnia [...]
The Bull Og » Blog Archive » The Bull Og Gets Monthchunky
[...] Just installed Justin’s Monthchunks. Sweet plugin. If you like it, be sure to send him some PayPal love like I did. [...]
Xavi Ivars
Will it work for WordPress 2.1?
justin
Xavi, I don’t know, I’ll test with the release candidate when I get a chance
Ann
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
justin
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).
hide10
It is fails in the judgment of “month in bold” when the archive exceeds 128 months.
justin
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.