Improved Related Posts in Sidebar

If you checked out the comments on yesterday's post — WordPress Related Posts in Sidebar — you'd have noticed that the "related" posts code I use was flawed. Namely, it didn't exclude the post you were on, so the chances were that only 4 out of 5 would be things you hadn't already read.

Anyway, I noticed this months ago when I first implemented the code, didn't see an &exclude= in the query_posts() section of the WordPress codex and promptly forgot all about it. Why fix what isn't even broken? I could have nested a few if statements in the code but this is a pretty botched solution, particularly when WordPress' native functions already contain so many options and alternatives.

Then, as I was getting dressed this morning (yes, I think about code pretty much ever hour of the day), I had a bit of a brainwave. If I switch from query_posts() to get_posts(), which I knew supported exclude, I'd be able to get rid of the current post from the list  :D So anyway, enough waffling, here's the improved code:

if (is_single()) {
	if (!isset($thispost)) $thispost = $post->ID;

	if (get_the_category()) {
?>
		<p>These similar posts from the same categories may be of interest to you:</p>
<?php
		foreach((get_the_category()) as $category) {
			echo '<h3>Related Posts in '.$category->cat_name.'</h3>'."rn<ul>";
			$posts = get_posts('numberposts=5&category='.$category->cat_ID.'&exclude='.$thispost);
			foreach($posts as $post) {
?>
				<li><a href="<?php the_permalink() ?>" title="permalink to <?php the_title() ?>"><?php the_title() ?></a></li>
<?php
			}
			echo "</ul>rn";
		}
	}
}

This sits nicely in the sidebar.  :D Of course, it should go without saying that if you haven't already got PHP in the sidebar which you can slot that in with, you'll need to wrap it in <?php   ?>

Comments (9)

  1. gravatar

    On Wed 10th Sep 2008 @ 20:53, Ben said:

    I don't necessarily think about code all the time, but I am always thinking about how to make websites more user friendly, cool new effects to try out... It's not weird at all >;)

  2. gravatar

    On Wed 10th Sep 2008 @ 21:01, Aisling said:

    Ha ha, nice Jem. I was thinking of putting something like this on my single posts page, because the sidebar is so empty and lonely.  :P

  3. gravatar

    On Wed 10th Sep 2008 @ 21:05, Vasili said:

    Why can they just make query_posts() support exclude or negative post ids?  :P

  4. gravatar

    On Wed 10th Sep 2008 @ 22:44, Meli said:

    I hope you can feel the epic amounts of love washing over you right now. This is EXACTLY what I'd been trying to fudge myself.

  5. gravatar

    On Thu 11th Sep 2008 @ 13:18, Arwen said:

    I don't think about code all the time, but I tend to think about it at odd times of the day. And I tend to giggle when I see the number 404 in real life. Slightly unrelated, I know. If I ever put related posts on my pages anytime soon, I'll refer to this.  :)

  6. gravatar

    On Thu 11th Sep 2008 @ 14:52, Stephanie said:

    You're nifty, Jem  ;)

  7. gravatar

    On Sat 13th Sep 2008 @ 03:49, Vanesa said:

    This isn't really about related posts in the sidebar, but LOL, I think about coding every hour of the day too!

  8. gravatar

    On Sun 14th Sep 2008 @ 15:38, wonkotsane said:

    Like it, using it.

    As an aside, you say "I'm not interested in viagra or penis enhancers" - could that be the cure for Karl's "Sir Bendy" problem?  ;)

  9. gravatar

    On Sun 9th Nov 2008 @ 07:20, Chris said:

    Thanks for the great code. Something I noticed though is that if you have a post categorised in 2 or more categories, the related posts list will display multiple times.

    Is there a way that if you do have a post in multiple categories, that the related post list will only display once?

    Many thanks!

Leave a Comment

No HTML (it will be stripped); first time comments are moderated; comments left without a legitimate first name or nickname will be deleted (no, your momma didn't call you "cheap quality wardrobes").








This entry was posted on Wed 10th Sep 2008 @ 17:38, and there are currently 9 comments. Leave your own?

Tags:

If you like this entry, you may also like:

« previous: Beginner's Guide to PHP - Part Five


Enjoyed this entry?
Then why not subscribe to my blog.