Category Based Related Posts For WordPress
Depending on the way you are using WordPress this may, or may not, work well for you. It kind of relies on the fact that posts will be related because they are in the same category. If for whatever reason posts can be unrelated to each other, although they are in the same category, you’ll probably want to use a plugin like YARPP.
The Idea!
We want to show the related posts at the bottom of the post on single.php
(kinda like I do), but use no plugins. To do this we are going to ask WordPress what category the current post is in, and then use it to make a custom loop. Got it? Good, let’s go.
The Code!
First we need to ask WordPress what category the current post is in.
1 |
$category = get_the_category($post->ID); |
This will give us back an array, with one entry per category, containing an object with that category’s information within it. We also need to know which post we are looking at so we can exclude it from our results.
1 |
$current_post = $post->ID; |
Now let’s add that together with a call to get_posts()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$category = get_the_category($post->ID); $current_post = $post->ID; $posts = get_posts('numberposts=3&category=' . $category[0]->cat_ID . '&exclude=' . $current_post); ?> <ul> <?php foreach($posts as $post) { ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php } ?> </ul> |
There we have it. In get_posts();
we have asked for 3 posts, from the current category, and excluding the current post. $category
is an array with an object inside hence the need for the [0] on the variable.
The Limitations!
Well as I said before one major limitation is that it will never be as good as a related plugin like YARPP due to the special scoring system it uses. However it is great for certain websites/blogs. You may also notice that it only gets posts from the first category attached to the post, you can do something about that should you want to.
1 2 3 4 5 6 7 8 9 10 11 |
$category = get_the_category($post->ID); $current_post = $post->ID; if(isset($category[1])) { foreach($category as $c) { $categories = $c->cat_ID.','; } } else { $categories = $category[0]->cat_ID; } $posts = get_posts('numberposts=3&category=' . $categories . '&exclude=' . $current_post); //Rest of code from before... |
That will create a comma separated list of the category IDs which is how get_posts()
expects it. It will however only do it should it detect that the category array has more than one entry saving a bit of time & memory.
Sadly I can’t provide an example for this one. However as always if you have any questions, or suggestions please let me know using the comments.
9 Comments
Psy
Hi, thanks a lot for the idea. But I’m having a little trouble with the code. I wanted to list all posts from the current post’s category and highlight the title of the current post in the list. To do that I changed your code to this:
But in the end: The links on the list that I get doesn’t change the content of the current post. The post and the URL, they both change, but the content stays the same.
Any ideas?
Thanks in advance.
Paul Robinson
Do you mean that
the_permalink()
is giving you the wrong link?You might need to run
setup_postdata($post);
just after the start of theforeach
. I’ve never had too on my own blog, but I know a few I’ve worked on have required it.If that’s not what you meant get back to me & I’ll see what else I can do to help.
Psy
No, the_permalink() is giving the right link, but the content just doesn’t change when you click on it. Anyway, instead of searching for a problem in the code, I found a different solution to my problem. Here, check it out:
Thank you, your article gave me the inspiration to do this. Take care.
Paul Robinson
Wow. Okay, I get what you meant now.
Nice way of working round it. 😉
Nicole
Is there a way to add the first image from each of the posts next to the title?
I am looking to make a list of each post under that particular category with its first image as a thumbnail. Could this work?
LisaMarieArt
Hi Nicole,
The easiest way to do this is by upgrading to WordPress 3.0 (if you are using a version less than 2.9 or haven’t already) and use the new custom thumbnail feature.
First you will need to activate the feature by editing the
functions
file. You can do this by following the instructions in our Custom Thumbnail Tutorial (at the bottom under the heading The Post Thumbnail) … This method is not retroactive and won’t make thumbnails for existing images (only ones you upload after you add the code). You’ll find a link to a plugin to auto generate the thumbnails for existing images in the tutorial.To display the thumbnail all you need to do is use the following in the place where you want it to show (remembering to keep it in the loop)
Just remember that the bit in apostrophes needs to be the same as the name you gave your custom thumbnail function.
If you want to make it a link wrap an <a> around it like the one in the sample code at the top of this page. Finally, use CSS and extra <div> if needed to style it how you want.
Nicole
Thanks so much! Do I have to use this on every post or can I use it on one post only (i.e. list other posts in that category on one post only)?
Paul Robinson
Well by the nature of WP templates it will automatically display on all single posts, but if you know the ID of the post you could wrap the entire code in a conditional like this:
Where ’11’ is the ID of the post you only want the code to display on.
You can also use the post slug (that’s the one that appears in the URL separated by hyphens) or the Posts title, but if you have two posts with the same title it can cause problems so I’d use the ID.
Hope that helps you out, and thank you to LisaMarieArt for answering before. 🙂
Keshav Naidu
Nice post mam, thanks.