Free Wordpress Blog Setup

How to Display Related Posts in WordPress

By Editorial Staff in Tutorials
How to Display Related Posts in WordPress

If you know anything about bounce rate, you are probably already showing related posts in WordPress. Bounce rate essentially represents the percentage of initial visitors to a site who “bounce” away to a different site, rather than continue on to other pages within the same site. This can have a significant impact on your adsense earning. By adding Related posts in WordPress you are killing three birds with one stone. You are decreasing your bounce rate, you are increasing your adsense income, and lastly you are increasing your pageviews. In this article we will show you how you can display related posts in WordPress with and without a plugin.

There are two methods of doing so and you can choose whichever method you prefer. One way is doing it without a plugin and then there is another method that is doing it with a plugin.

Display Related Posts in WordPress without a Plugin

Paste the following code in single.php where you want to display related posts

<?php
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

<?php
endwhile;
}
wp_reset_query();
}
?>

Source: MichaelH

Display Related Posts in WordPress with a Plugin

The plugin we use is called Yet Another Related Post Plugin (YARP). It gives you a list of posts and/or pages related to the current entry, introducing the reader to other relevant content on your site. Key features include:

  • An advanced and versatile algorithm: Using a customizable algorithm considering post titles, content, tags, and categories, YARPP calculates a “match score” for each pair of posts on your blog. You choose the threshold limit for relevance and you get more related posts if there are more related posts and less if there are less.
  • Templating: New in 3.0! A new templating system puts you in charge of how your posts are displayed.
  • Caching: New in 3.0! YARPP now caches the related posts for all the posts and thus has significantly improved performance over versions 2.x.
  • Related posts in RSS feeds: Display related posts in your RSS and Atom feeds with custom display options.
  • Disallowing certain tags or categories: You can choose certain tags or categories as disallowed, meaning any page or post with such tags or categories will not be served up by the plugin.
  • Related posts and pages: Puts you in control of pulling up related posts, pages, or both.

Download YARP

What Next?

Digg it
Save This Page
Subscribe to WPBeginner
Stumble it
Free Wordpress Blog Setup

Comments

17 Responses to “How to Display Related Posts in WordPress”
  1. Ileaneb says:

    Does this work for wordpress.com as well? Are the instructions the same?

  2. Vin says:

    I’ve been looking for a way of adding posts related to specific tags on the bottom of pages I have created. EG: Posts that are related to the pyramid of Teti…
    http://www.pyramidofman.com/blog/tag/teti/

    …on the bottom of the Pyramid of Teti page:
    http://www.pyramidofman.com/blog/saqqara/pyramid-of-teti/

    At the moment I’ve just added the tags at the bottom of tghe page but would be much better to have exerpts of the related posts added.

    Is there a way i can adapt the code that you have given to do this?

    Thanks.

    @Sergej,
    Do you have a link?

  3. palmamod says:

    Hey, I implemented the non-plugin version of this and am 99% happy with it. I know it’s not you who have developed it but maybe you could help. If you check the posts on my blog, you will see that 5 posts are displayed (matching the first tag of the post) but “in line”. Do you know how can I tweak this so that a list occurs? Like, tweaking the php so that a and tags are created?
    Thanks a lot and keep up the good job!

  4. Sal Surra says:

    Great tip! Works perfectly and thanks for sharing this. I like the tip as it shows my recent posts in a nice lists view.

  5. Excellent, buy how can i put a thumbnail? Thanks.

  6. matthew says:

    Great tip! thanks for sharing this

  7. Vincent says:

    (revised comment)
    I’ve been looking for a way of adding POSTS related to specific tags on the bottom of PAGES I have created. EG: POSTS that are related to the pyramid of Teti…
    http://www.talkingpyramids.com/tag/teti/

    …on the bottom of the Pyramid of Teti PAGE:
    http://www.talkingpyramids.com/saqqara/pyramid-of-teti/

    At the moment I’ve just added the tags at the bottom of the PAGE but would be much better to have exerpts of the related POSTS added.

    Is there a way I can adapt the code that you have given to do this?

    Thanks.

  8. Chris Roane says:

    Thanks for providing the code. I needed something that was similar to this, but that took into account all of the tags for a post to generate the related posts.

    So I modified the code and posted it on my website. I did put a link to this page in the article. Here is the article that I wrote: http://www.montanaprogrammer.com/php-web-programming/add-related-posts-to-wordpress/

  9. brynh says:

    Very good article! It helped me much! Thank you.. I installed YARP and it works great..

  10. Paisakamana says:

    Really great piece of code you got there.
    Great article I used this on my site and I like.

  11. Sounds quite useful. Thanks for the information.

Share Your Opinions

Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!

Please make sure that you have read our Comment Policy.

Due to high volume of request from our readers, we are adding this feature that allows you to stay updated with this post's comments without having to participate in the discussion even though we would love your input as always. Don't worry we hate SPAM just as much as you do, so you will never receive any SPAM messages from our site and that's our promise to you.

Subscribe without commenting

Close Bar