WordPress Related Posts in Sidebar

Several of you keep pestering me on how I do my 'related posts' thing in the sidebar. You're nosy buggers, I'll give you that. Anyway, here comes the confession part: they're not really "related" posts at all. In fact, the only thing that they each have in common is that they're in the same category. My related posts are the last 5 posts from each category that the post you're viewing is in (whew, mouthful).

In sidebar.php, I check that the post is exactly that: a single post. I can then grab the category IDs, loop through them, and grab the post information as I go. Blah blah blah, the interesting (working) part is:

if (is_single()) {
	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>";
			query_posts('cat='.$category->cat_ID.'&limit=10');
			while (have_posts()) {
				the_post();
?>
				<li><a href="<?php the_permalink() ?>" title="permalink to <?php the_title() ?>"><?php the_title() ?></a></li>
<?php
			}
			echo "</ul>rn";
		}
	}
}

You're all welcome to help yourself, of course.

Comments (12)

  1. gravatar

    On Tue 9th Sep 2008 @ 17:58, Josh said:

    Thanks for sharing. How might the current post be excluded from the list? For example, the sidebar of this post lists this very post as a "related post".

  2. gravatar

    On Tue 9th Sep 2008 @ 18:37, Jem said:

    ^ It doesn't, although I really should mod' it so that it does. Which I guess is what you're asking... let me have a play and I'll get back to you.

  3. gravatar

    On Tue 9th Sep 2008 @ 20:25, Vasili said:

    @Josh You could probably compare the ID of the post in the loop to that of the post on the page. If they are the same you can skip that once around in the loop with continue.  :)

    I don't know how you'll be able to code that, but that's probably what you're aiming for.

  4. gravatar

    On Tue 9th Sep 2008 @ 21:02, Jem said:

    if ($post->ID ... etc

    but I want to see if WP has it built in first .. something like &exclude=# (because they normally do)

  5. gravatar

    On Tue 9th Sep 2008 @ 21:31, Vasili said:

    You can change the query to, query_posts('cat='.$category->cat_ID.'&limit=10&offset=1');

    I'm pretty sure that will work.  ;)

  6. gravatar

    On Tue 9th Sep 2008 @ 22:03, Jem said:

    Only if you're on the latest post in the category. If you're in another post in the same category and you offset by one, you'll be missing a different post.  :P

  7. gravatar

    On Wed 10th Sep 2008 @ 01:08, Mat said:

    Hmm..

    Youd expect something like:
    query_posts('cat'='.$category->cat_ID.'&id=-'.$post->id.'=&limit=10');
    would work, however it doesnt.

    Perhaps you should patch it so it does then commit it back to wordpress's trunk.
    (or I will  :P)

  8. gravatar

    On Wed 10th Sep 2008 @ 02:34, Aaron said:

    Finally!

    Thank you for this. I've tried plugins and other codes, but they always phail. I will be forever grateful. Or at least grateful for a few days.

  9. gravatar

    On Wed 10th Sep 2008 @ 11:34, Jem said:

    Found a fix, will post later  :D

  10. gravatar

    On Wed 10th Sep 2008 @ 17:30, Jabed said:

    Hi Jem, perhaps you could add these mini tutorials onto your tutorialtastic website. Speaking of that website when are you going to add some more tutorials  :)

  11. gravatar

    On Wed 10th Sep 2008 @ 20:43, Improved Related Posts in Sidebar — jemjabella.co.uk said:

    [...] 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. [...]

  12. gravatar

    On Sat 20th Sep 2008 @ 01:28, Alessandro said:

    So good! It can be a widget.
    I think I will do it
    thanks Jem!

Leave a Comment

No HTML (it will be stripped); 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").