Previous and Next Month Links in Archives

It has just taken me around an hour to try and insert some previous and next month navigation links to the blog monthly archives. It seems that WordPress doesn’t have any real native support for this time of navigation which is odd.

Anyway the code below is my very hacky version of how to do it. I placed this in my theme’s archive.php file

<?php //twentyeleven_content_nav( 'nav-above' ); ?>
<nav id="nav-above" style="display:inline">
$archive_year = get_the_time('Y');
$archive_month = get_the_time('m');
$archive_month = $archive_month - 1;
if ($archive_month == 0) {
$archive_month = 12;
$archive_month_next = $archive_month + 2;
$archive_year_next = $archive_year;
if ($archive_month_next > 12) {
$archive_month_next %= 12;
echo '<a href="' . get_month_link( $archive_year, $archive_month) . '">Previous Month</a>';
if ($archive_year_next.sprintf('%02d', $archive_month_next) < = date('Y', strtotime("now")).date('m', strtotime("now"))) { echo '<a href="' . get_month_link( $archive_year_next, $archive_month_next) . '" style="float:right">Next Month</a>'; } ?> </nav>

Replacing titles with captions in WordPress gallery image links

Recently I have installed a colorbox plugin onto this blog for image galleries.

This plugin overrides the default behaviour of an image gallery and replaces individual image pages with a simple in-page pop up of the image. It works quite nicely, however, one problem I have had is that it extracts the title attribute from the former page links rather than the caption of the image that it is displaying.

I have spent the last few days trying to figure out a way to get the behaviour I want out of the plugin. However, this has been much more difficult that expected, the code that generates the galleries in WordPress is buried deep in the code and isn’t as straight-forward as replacing title=getTitle with title=getCaption. Trying to edit the behaviour of the jquery on the colorbox plugin isn’t an option either as it is heavily optimised. And updating every database row to replace the title with the caption also isn’t very¬†practical.

Instead the simple hack below will achieve what I want. It isn’t pretty and I am still looking for a better solution, but in the meantime after the page has been rendered the jquery quickly looks for galleries and replaces the titles in any links with the alt text from the image. This means that when an image loads in the colorbox it displays with a caption.

<script type="text/javascript">
jQuery(document).ready(function($) {
 $('.gallery img').each(function() {