Wordpress: How to Customize the Date Format for wp_get_archives
Monday, August 17, 2009

The wp_get_archives template tag is one you’ve likely seen before—it displays date-based archives in a list much like what you see in my sidebar under “Explore.” Surprisingly, the parameters accepted for this tag allow no changes in the date format.
It’s difficult to customize this right from your theme, and I suspect that’s why I was unable to locate any flexible solutions through the Wordpress forums or Google. The easiest way is to edit the original function itself. Be warned: new Wordpress installations will overwrite this fix.
Locate general-template.php
Open the wp-includes folder from the root of your Wordpress install. Open the file general-template.php with a text editor and locate the wp_get_archives function. Find the following line:
820 821 | $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year); // |
In Wordpress 2.8.4 this is on line 820. The default date format is the full textual representation of the month and the full year. Example: January 2009.
Alternative Date Formats
Short textual representation of month
Example: Jan 2009
820 821 | $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month_abbrev($wp_locale->get_month($arcresult->month)), $arcresult->year); // |
Numeric representation of month with leading zero
Example: 01 2009
820 821 | $text = sprintf(__('%1$s %2$d'), zeroise($arcresult->month, 2), $arcresult->year); // |
Numeric representation of month, no leading zero
Example: 1 2009
820 821 | $text = sprintf(__('%1$d %2$d'), $arcresult->month, $arcresult->year); // |
Two digit representation of year
Example: January 09
820 821 | $text = sprintf(__('%1$s %2$s'), $wp_locale->get_month($arcresult->month), substr($arcresult->year, 2)); // |
Adding Flourishes and Changing Order
Additional formatting can be applied by just changing ‘%1$s %2$d’ in that line, where %1$s represents the month and %2$d represents the year.
Punctuation between month and year
These examples use a slash. Replace with dot or dash as desired.
Example: 01/2009
820 821 | $text = sprintf(__('%1$s/%2$d'), zeroise($arcresult->month, 2), $arcresult->year); // |
Example: 01/09
820 821 | $text = sprintf(__('%1$s/%2$d'), zeroise($arcresult->month, 2), substr($arcresult->year, 2)); // |
Abbreviated Year
Example: January ‘09
820 821 | $text = sprintf(__('%1$s /'%2$s'), $wp_locale->get_month($arcresult->month), substr($arcresult->year, 2)); // |
Reversed Month and Year
Example: 2009 January
820 821 | $text = sprintf(__('%2$d %1$s'), $wp_locale->get_month($arcresult->month), $arcresult->year); // |
Mix, Match, Enjoy!
There are more combinations than I can reasonably post here, but I hope this helps you figure out how to get the exact date format you want.








Comments
7:29 PM
Thank you for this! I am trying to start my own Wordpress blog and these tips really come in handy.
6:24 AM
your solution leads to change the core of WP… for many reason this isn’t a great idea…
11:14 PM
Thanks for reminding us, Arif. I’m not sure about “many reasons,” but here’s where you have to be cautious:
A) You don’t want to screw up anything on the back-end so follow the directions precisely. Luckily, if you do make some kind of egregious error that you can’t fix, you can always just download the WP install again and overwrite your modified general-template.php.
B) As forementioned, new Wordpress installations will override the fix.
It’s not an ideal solution, but unfortunately Wordpress doesn’t facilitate one—and a janky solution is preferable to none!
10:49 PM
Thanks for this Allison, it was exactly what I needed.
I have to believe that this sort of functionality can be added using a plugin so that it’s independent from the WordPress core. I may poke around and see if I can come up with something.
6:46 PM
That would be great! I’d love to see a plugin or other template-oriented solution out there. The couple I’ve seen have been pretty messy and impossible to customize for the casual user. Let me know if you come up with anything—I’ll link it here!
2:17 AM
That was some interesting stuff there. Thanks for posting it.
10:07 PM
Great! Thanks for the tips. This led me to experimenting with solution that does not touch the core of WP. I managed to come up with two solutions. The first one is a COPY-AND-PASTE method. The second is the FILTER method.
1. COPY-AND-PASTE Method
Basically, I just copied the function for the archive list from general-template.php and pasted it in my theme’s function.php.
Then change the function name from wp_get_archives to my_wp_get_archives, for example. So you call the archive list using the latter one.
To make it more efficient, I deleted the lines that I don’t need.
This way I can modify the code as I like, using your tips.
Here’s a link to my modified function:
http://www.zumodrive.com/share/3Mr0NGQzMT
Change line 53 to customise the date (using your tips).
Change lines 71 to 78 to customise the format (according to your needs).
2. FILTER Method
For the second method, I tried using the WP filter feature. This one retains the function name (wp_get_archives). Also, put this in your function.php.
I have 2 examples. One allows changing the dates only. The other allows changing the whole format. Both examples displays the dates in numerical format with a slash as delimiter.
a. Custom dates:
http://www.zumodrive.com/share/3Mp1N2Q4Y2
b. Custom format
http://www.zumodrive.com/share/3MrXNzk3YT
Of course, you can be creative with the code.
These are only examples of what you can do. They may not be the best, but they work ;)… and you don’t have to change any core coding, too!!
Cheers.
1:08 AM
Thank god for the last commenter. There is ALWAYS a way to do it cleanly and without having to hack the core system. ALWAYS. :)
If you ever need help, hit me on teh twitter.
1:50 PM
Great! This helped me sooooooo much!
5:19 AM
[...] Thank you, Allison! (View her site) [...]
4:27 AM
We hatch a open-handed row of hair extensions and sell them here shortly in our plant betray, theres no bull’s-eye check so the total you pay the way for on sales marathon here is at heavily discounted prices. We produce sole real remy hair. You wont on counterfeit hair here as it just doesnt match up the distinction of our 100% human hair.
If your second-rate urgent visionary looking personality pattern tresses without any of the severe m‚tier then youve befall to the without delay place. Our most acclaimed artefact is our domain renonwed punch in plaits extensions.
Wearing shorten in curls extensions is relaxed! Starkly stimulus your locks, express in the clasp, secure it closed and you’re done. After putting them in afew times youll soon get against to them, in advance lengthy youll be able to associate your fraction extensions with your eyes closed!.
Clip in’s are by far the easiest method for terrific flash mane and it comes without any commitments, entirely take them discernible when you dont call for them, away with them at liberty at prohibited end of day and disdain them in again when your perceptive!
We also distribute pre bonded tresses fit salons or for those of you who knows how to fritter away them (seasoned tresses stylists recommended). Weaves in support of those of you who hanker after to disseminate on your own clips. At length our latest combining (coming anon) is our entire chewing-out share hair’s breadth extensions in requital for the fastest instant look.
Knock off adjust to look via our braids extensions, if you contain any questions only just require our persevere group after succour, if we dont have what you for disenchant us be acquainted with and we can indubitably initiate it due to the fact that you in our plant Hair Extensions!
http://www.superstrands.com/
Post a Comment
Required fields are marked with †. Inflammatory comments will be removed. Your courtesy is appreciated!