Ever since I adopted the current design for my monthly archive pages (one that lists all my posts for that month by title, rather than paginated excerpts—Matt blogged about this approach back in the day), I’ve wanted a convenient way to page back and forth through my archives by month—without having to blindly hard-code links for
$current_month+1. I discovered a few people asking for the same thing, and I found an out-of-date plugin that had attempted to accomplish it, so I decided to write my own from scratch, as part of my recent redesign. Here’s how it looks in context:
Screenshot of Adjacent Archive Links
- Download Adjacent Archive Links v2.0 from the WordPress.org Plugin Directory (or from me here)
- Unzip the file and upload the folder
- Activate the plugin
- Add the new template tags (described below) to the appropriate template file for your theme (e.g. archive.php)
After activating the plugin, you will have two new template tags at your disposal:
<?php previous_archive_link( 'format', 'link' ); ?> <?php next_archive_link( 'format', 'link' ); ?>
They adapt their output depending on whether the date archive is a day, a month, or a year, and they will only output a link for the previous/next-most time period in which there is a published post. If there are none, it will output nothing. Both tags take the following two parameters (which were based on the core WordPress template tags:
- (string) The format parameter defines what comes before and after the link. In the string,
%linkwill be replaced with whatever is declared in the link parameter below.
- (string) The link parameter controls the link text. Both tags default to
%date, the adjacent archive page’s localized date.
Questions, comments, and suggestions are always welcome. If you’re interested in contributing to the code behind Adjacent Archive Links, it’s hosted on GitHub.