Sep10, 2008

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>'."\r\n<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>\r\n";
}
}
}

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   ?>

Tagged .

Comments

There are currently 9 approved responses to "Improved Related Posts in Sidebar".

  1. [gravatar]

    Ben 10/09/08 at 19:53 #

    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]

    Aisling 10/09/08 at 20:01 #

    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]

    Vasili 10/09/08 at 20:05 #

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

  4. [gravatar]

    Meli 10/09/08 at 21:44 #

    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]

    Arwen 11/09/08 at 12:18 #

    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]

    Stephanie 11/09/08 at 13:52 #

    You're nifty, Jem ;)

  7. [gravatar]

    Vanesa 13/09/08 at 02:49 #

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

  8. [gravatar]

    wonkotsane 14/09/08 at 14:38 #

    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. Chris   09/11/08 at 07:20 #

    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!

Comments are closed.