Upgrading to WordPress 1.5.1 on highinfrequency.com was pretty painless. Unfortunately, the upgrade broke a tiny yet really important piece of the site navigation. The short story is that I fixed it with a couple hacky lines of PHP code. If you’ve read this far, you might be interested in the longer story (including the hack PHP which you are welcome to critique ridicule). If you are in fact interested in the longer story, it continues after the jump…

Update: There is a better fix for this problem posted here(update 2007-08-02: blueblurry.com is no longer around, removed link). It is not plagued with the same problems I describe below. Thanks Pat!

So, on the site a simple previous/next navigation style is used. To accomplish this, the WordPress previous_post() and next_post() functions were used. This worked fine in version 1.5, but in 1.5.1, the developers made these functions work as they are documented. This means that previous_post() and next_post() only work on single pages and not on pages like the home page or archive pages. Bummer. Of course, I was not the first to come across this problem, someone else had already started a thread describing the same problem. Unfortunately, there were no answers there, and I could not find any elsewhere.

After trying to use some other WordPress functions to accomplish the same thing, I decided to just make a hack that would work on this site. Rather than use the previous_post() function on my home page to show the permalink back to the prior post, I used this code:

	$prevpostid = --$post->ID;
	print '&lt; <a href="' . get_permalink($prevpostid) . '">previous</a>';

This works fine for what this particular site needs and might work for other photoblogs having the same issue. Before you cut and paste this into your template, you should probably know about a couple problems with this simple workaround:

  1. This is using some really simple arithmetic on the current post ID to come up with the previous post ID. Simply subtracting 1 will not always yield the desired results. For example, if you use the “page” function in WordPress to maintain pages outside the blog structure and the post ID minus 1 actually refers to a page, the permalink to that page will be the “previous” post.
  2. If your posts are not in chronological order, some really strange stuff is going to happen if you use this code. The good news is that if the post before the current one (according to the ID numbers) is not published (“draft” status), the get_permalink() function gives the permalink for the next available post.
  3. For the same reasons mentioned above, using this on archive pages is probably going to give some really wild results. This would not be recommended.

There is definitely a need for some improved previous/next functions in WordPress. A plugin that handles this type of thing a little more gracefully would be a welcome addition to the plugin library for sure.

Leave a Reply