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
?>
10 Sep at 7:53 pm
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 >;)
10 Sep at 8:01 pm
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
10 Sep at 8:05 pm
Why can they just make query_posts() support exclude or negative post ids? :P
10 Sep at 9:44 pm
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.
11 Sep at 12:18 pm
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. :)
11 Sep at 1:52 pm
You’re nifty, Jem ;)
13 Sep at 2:49 am
This isn’t really about related posts in the sidebar, but LOL, I think about coding every hour of the day too!
14 Sep at 2:38 pm
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? ;)
09 Nov at 7:20 am
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!