Adjacent Archive Links for WordPress

WordPress logoEver 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 and $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
Screenshot of Adjacent Archive Links

Instructions

  1. Download Adjacent Archive Links v3.0 from the WordPress.org Plugin Directory (or from me here)
  2. Unzip the file and upload the folder adjacent-archive-links to your /wp-content/plugins directory
  3. Activate the plugin
  4. 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: previous_post_link and next_post_link):

format
(string) The format parameter defines what comes before and after the link. In the string, %link will be replaced with whatever is declared in the link parameter below. previous_archive_link defaults to &laquo; %link and next_archive_link defaults to %link &raquo;.
link
(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.

Update: Plugin en el español por Andrew de WebHostingHub.

Feel free to if you found this useful.

8 Comments

I installed your plugin, which would appear to be exactly what I need. I then pasted the template tags into archive.php Instead of “next month” and “previous month” I see the following at the bottom of each archive

$year = $archive_month[0]->year;
$month = $archive_month[0]->month;
$full_month = date("F", strtotime("$year-$month"));
$label = str_replace(array('%month','%year'), array($full_month,$year), $label);
return ''.$label.'';
} return '';
} formatformat

What have I done wrong.

see http://www.lgrossman.com/trudel/1936/04/

Thanks,
Len

Sorry. The previous error seems to have been my fault.

I have now pasted

into my archive.php. But this merely results in “formatformat” (without quotes) appearing at the bottom of my archive pages. Do I need to do something else? I use monthly archives.

Thanks,

Len

Lenoard, just paste the following into your archive.php template:

<?php previous_archive_link(); ?>
<?php next_archive_link(); ?>

The 'format' and 'link' params are means as placeholders for parameters that give you the ability to customize the output of the template tag. You can just leave them out, and in most cases, the defaults should be good enough.

Thanks, I did and it works perfectly. I added “. . .” without quotes, between the lines to separate the previous date from the following date. I am sure there must be a more elegant way, but in the meantime this works. You can see the result at the bottom of http://www.lgrossman.com/trudel/1935/07/ for example.

Heading over to your donate button right now. Thanks again.

Len

Hello, very nice plugin – but there is a problem with non english languages as the plugin does not take in count the language of the WP installation. Can you correct this please ?

Andrew Kurtis

Hi Justin,

My name is Andrew, I am with WebHostingHub Support. We would like to translate your Adjacent Archive Links plugin that we found at http://justinsomnia.org/2012/11/adjacent-archive-links-for-wordpress/ to Spanish language, no charge of course, to help people from Hispanic community better manage their websites. Would that be ok with you?

I hope I’ll hear from you soon.

Kind regards

Andrew Kurtis
WebHostingHub Support

Andrew, thanks for the interest. I sent you an email, but for anyone else who’s interested, the best way to contribute would be to fork my repo on GitHub (wp-adjacent-archive-links) and send me a Pull Request.

Ashish M.

Hi Justin,
It is posible to use previous_archive_link(); in page template not in archive.php

Care to Comment?

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

Name

Email (optional)

Blog (optional)