I was just thinking the other day that date-based archives in most WordPress blogs don’t get enough exposure unless the blog owner uses a widget to show a list of archives. Since I’m not a big fan of using long lists of archives in the sidebar and wasting valuable space, this method was out of the question for me.
But, I still wanted a way to easily allow readers to visit my archives. I also wanted to not just show monthly archives like the standard WordPress archives widget. The method I decided on has three major benefits:
- Allows readers to find archives by year, month, and day.
- Doesn’t take up any extra space on my blog.
- The code was already written in my Breadrumb Trail plugin, so there wasn’t much I had to do.
On this blog and many others, we have a thing called a byline or dateline, which is usually located before or after the post title. Traditionally, the date the post was written is shown. But, it doesn’t do much. It just sits there. Yes, it provides valuable information to the user but not anything more than that.
What we’re going to do is link the various parts of the date to archive pages. We’re going to give the post date more purpose.
Creating the post date link function
The first thing we need to do is create a function in our theme’s functions.php file. Actually, we’re going to create a function and a shortcode. Just drop this PHP code in the file:
<?php
add_shortcode( 'entry-link-published', 'my_entry_published_link' );
function my_entry_published_link() {
/* Get the year, month, and day of the current post. */
$year = get_the_time( 'Y' );
$month = get_the_time( 'm' );
$day = get_the_time( 'd' );
$out = '';
/* Add a link to the monthly archive. */
$out .= '<a href="' . get_month_link( $year, $month ) . '" title="Archive for ' . esc_attr( get_the_time( 'F Y' ) ) . '">' . get_the_time( 'F' ) . '</a>';
/* Add a link to the daily archive. */
$out .= ' <a href="' . get_day_link( $year, $month, $day ) . '" title="Archive for ' . esc_attr( get_the_time( 'F d, Y' ) ) . '">' . $day . '</a>';
/* Add a link to the yearly archive. */
$out .= ', <a href="' . get_year_link( $year ) . '" title="Archive for ' . esc_attr( $year ) . '">' . $year . '</a>';
return $out;
}
?>
For non-American users, you might want to switch the dates around as I’m using the “Month Day, Year” format. The WordPress Codex also has a helpful guide on formatting date and time if you want to mix things up a bit.
Note to theme authors: Please don’t implement this function as is into your themes. There are tons of variations on the date/time format and we have to respect what the user chooses unless the design can’t be accomplished without a specific format. If you come up with a way to handle all scenarios, then by all means, go for it.
Using the published link in your theme
We’ve added the function to run this to our theme’s functions.php file. Now, we have to use the function in our theme. All themes are going to be a little different, so I can’t tell you exactly where to put the code. You’ll just have to look in your theme templates or ask your theme author how to do it. I can tell you that this code should go within The Loop.
This is the code you add to the template:
<?php echo my_entry_published_link(); ?>
That’s all the code you have to deal with.
If you’re using a super-intelligent theme like Hybrid, you can use the [entry-link-published] shortcode and not worry about adding the PHP function.
Or, if you’re not using Hybrid and just want to know how to run a shortcode in a theme template, try this out:
<?php echo do_shortcode( '[entry-link-published]' ); ?>
What are the benefits of doing this?
I count two main benefits:
- Providing an easy way for readers to visit your archives. It never hurts to implement more methods of keeping people on your site.
- It’s just something fun to do. Maybe spruce up your blog a little.
Maybe it’ll even help search engines crawl your site easier. I don’t know. I quit trying to figure them out a long time ago.
I hope you enjoy this little snippet of code. If you want to see it in action, I’m using it on my blog now. Just look at my post byline and you’ll see the date linked to three different archives.

Nice trick… I have implemented something like this on my half-past-dead blog, and though I have gone a different road, the result is the same, I explained it here.
Very cool — thanks for sharing this! Certain types of record-keeping sites where the date is more crucial may still want prominent archives lists in the sidebar, but otherwise I agree that this is largely a waste of front-page real estate (and I’ve never understood the appeal of calendar widgets). However, I think most blogs with regular readerships will still want either a drop-down list in the sidebar or an archives directory page for the odd reader who’s looking for a post that appeared on roughly such-and-such a date, especially considering how poorly WordPress’ native search function works.
This looks cool. I have always wondered about this issue.
Might just be a bit too techy for me.
Still I need to have a go, or get someone to help me!
Opal
The purpose of this code is really nice. You’re right, “It never hurts to implement more methods of keeping people on your site”, and this is another great way to do it. Better bounce rate and improves internal linking as well.
I’m a bit surprised these functions don’t default to the current post’s date. I guess if that were the default the function names would be different, like “get_the_month_link()”? Still a bit rough on WP naming conventions.
That’s a very interesting take on user retention. I agree that the default dateline doesn’t do a great deal, apart from visually.
Looking at your post thumbnail, I was wondering if it might be possible to generate something similar (including the lifted corner) using CSS3 transforms…
Great job….amazing post!!!Looking for another one…Best of luck
Very nice idea! Just a plug to one on of my own plugins. My collapsing archives plugin gives you a nice widget in the sidebar which can be configured to not take up much space. It can expand and collapse months and years, and also display posts if you want it to.
Using the publishing link in your theme is pretty darn good. I will need to take a deeper look.
Just out of curiosity Justin, who else other than the Americans use the month/day/year format? It is a bit confusing because sometimes you don’t know the actual date if it is like 06/05/1997.
ya every time i try and look at the date liek that it gets confusint as most people format the date ina different way. however im a firm believer that more tracking is better. so hopefully this works out
Thanks for a simple way to do this. I was thinking about doing this myself, but my technique had sql queries involved. thanks for simplifying it for me
Or, you could just use WordPress’s get_option(‘date_format’) to get the appropriate date format for the blog.
That would do nothing in the context of this tutorial without writing out a complex function to handle all of the possible link options.
Well, a like a little challenge. I think this covers the majority case:
That got a bit mangled, but I think you can see the gist.
Yep, much more complex and slower.
I’d still love to see a full solution that covered every possible date output though. I just haven’t had a need to write the function.
Most things can be slimmed down with a little bit of effort, but optionality almost always has an overhead. Anyway, I’ve since realised that this can be made simpler:
if(preg_match('/[FmMn]/',$dateformat[$x])) {
$out .= $monthly;
}
etc.
hmmm…I’ve had my dates link to my archives for a couple years now… BUT… everything is hardcoded. As in I don’t use date_format. Which bums me out. For the sake of learning I’m trying to get my theme as end user friendly as possible. Part of that would be allowing the user to change the date format as they see fit….. but I don’t want to give up the linking. So in the end, I’d love to see a way to always link the date to the archive, while using date_format to respect the users needs
I removed the archive widget from menu, because I set the permalinks to show only %post title% in URL, but in archives is including the date too, giving a 404 Error Page.