September 10, 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   ?>

Comments

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

  1. Ben   September 10th, 2008 [gravatar]

    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. Aisling   September 10th, 2008 [gravatar]

    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. Vasili   September 10th, 2008 [gravatar]

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

  4. Meli   September 10th, 2008 [gravatar]

    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. Arwen   September 11th, 2008 [gravatar]

    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. Stephanie   September 11th, 2008 [gravatar]

    You're nifty, Jem ;)

  7. Vanesa   September 13th, 2008 [gravatar]

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

  8. wonkotsane   September 14th, 2008 [gravatar]

    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   November 9th, 2008 [gravatar]

    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 Reply

No HTML please; first time comments are moderated; comments left without a legitimate first name/nickname and e-mail will be deleted (no, your momma didn't call you "cheap quality wardrobes").

[geek inside avatar]Welcome to the blog of girl geek & php ninja extraordinaire 'Jem'. Web developer, mum and supreme crazy cat lady.. »

---

Post stats

Posted on September 10, 2008 in with 9 comments

---

Related posts

Other posts you might be interested in:

---

Share this post

Talk about this post on your favourite social media website:

tweet this add to stumbleupon add to delicious add to reddit digg this post add to newsvine share on facebook share on friendfeed

---

Subscribe

Like this post? Why not subscribe to the feed?

Knowledge

Pleasure

Archives

See more...