How to display related posts in WordPress

How to display related posts in WordPress

In this article, i show you how to add and display related posts on your WordPress site by category and tag without a plugin. Its not good to always use plugins to automate stuff on WordPress sites.

I’ve never liked this approach and i try to stay away from it as much as i can unless i have no option left. You wanna know why? Activating a plugin means relying on the author for a service.

If the plugin author doesn’t use the best security standards while creating a plugin, its likely to get your website hacked. Plugins can slow down your websites as well. Some end up conflicting with other vital plugins. There are so many disadvantages of using plugins on a website and like mentioned earlier, only use them when you have no option.

For this case, we do have an option that requires some basic PHP knowledge. If you have no experience with coding, you could get a service from Fiverr or perhaps hire me. I can add related posts to your theme at a small cost.

One good thing with writing your own code is the ease for customization; call it flexibility. You can always add more stuff and make tweaks directly from the theme.

So allow me explain how to get related posts by category and by tag but before we go any further, i advise you make all these changes on  your child theme. It’s easier and safer that way. It also keeps your changes even after updates.

Get Related Posts by Tag

To get WordPress related posts by tag, you simply have to paste the code below where you want the related posts to appear in your theme’s single post template single.php file.

If your theme uses hooks to display various sections of the site and you are finding it hard to get a suitable position for the related posts, you could think of pasting the code right at the beginning of your comments.php file. This queries the posts right before your comments section.

The only problem with this is you have to ensure the code gets loaded in the single view only. You can achieve this by using conditional statements. Please take this point seriously else your site might get broken. If you have no programming knowledge, stay away from this option and paste the code in the single.php file instead.


<?php // My Related posts function function my_related_posts() { global $post; $tags = wp_get_post_tags( $post->ID );
    if($tags) {
        foreach( $tags as $tag ) {
            $tag_arr .= $tag->slug . ',';
        }
        $args = array(
            'tag' => $tag_arr,
            'numberposts' => 3, /* You can change this to show more */
            'post__not_in' => array($post->ID)
        );
        $my_related_posts = get_posts( $args );
        if($my_related_posts) {
        echo '

<h4>Related Posts</h4>


';
        echo '

<ul id="related-posts">';
            foreach ( $my_related_posts as $post ) : setup_postdata( $post ); ?>
                

<li class="related_post">
                    <?php echo the_post_thumbnail(array(150,100)); ?>
                    <a class="entry-unrelated" href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>


<?php the_excerpt(); ?>

                    
                </li>


            <?php endforeach; }
            }
    wp_reset_postdata();
    echo '</ul>


';
}
?>


The code above is similar to Eddie Machado’s “Bones” WordPress theme. The only addition i made was the excerpt and post thumbnail to give it a catchy look. Try is out and it should look awesome 🙂

You can also go ahead and style it to look more better.

Get Related Posts by Category

The code below displays random related posts by a category. Place it in your single.php file or right above your comments.php file but (wrapped in if else conditional statements).


<?php $orig_post = $post; global $post; $categories = get_the_category($post->ID);

if ($categories) {
   $category_ids = array();
       foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
       $args=array(
                    'category__in' => $category_ids,
                    'post__not_in' => array($post->ID),
                    'posts_per_page'=> 4, // Number of related posts that will be displayed.
                    'caller_get_posts'=>1,
                    'orderby'=>'rand' // Randomize the posts
       );

$my_query = new wp_query( $args );

   if( $my_query->have_posts() ) {
       echo '

<div id="related-post">
';
             while( $my_query->have_posts() ) {
                    $my_query->the_post(); ?>
                    

<li>                      


<div class="related">
<a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</div>

</li>

<?php } ?>

<?php echo '</ul>

</div>


';
} }
$post = $orig_post;
wp_reset_query(); ?>

When you should use Plugins

The good side of using plugins is you do not need to write any code. By default, the codes above will inherit the css of your theme which in most cases do not bring out a good UI. You will need to style the elements yourself.

A plugin on the other side comes with its own CSS. In fact, some plugins make it easy for you to customize the look of your related posts directly from your dashboard. So if your hosting package is good enough and you have less plugins installed on your site, you could just use a plugin to simplify this task for you.

However, endeavor to only install plugins with high rating and recent updates. Below are some of the few plugins that i personally recommend.

1. Jetpack Related Posts Module

2. Yet Another Related Posts Plugin

3. Contextual Related Posts

4. Related Posts by WordPress

5. Inline Related Posts

Benefits of having related posts on your website

There are so many benefits of having related posts on your website. These range from SEO benefits to offering a good UI to your users. We shall try to look at these benefits in detail.

Keep your visitors around

When visitors read an article on your website, they are likely to leave when they are done. Question is, how do we keep them around longer? Having related posts will help you keep them longer and read more posts.

This is not the case with a website without related posts where visitors look around and do not see what to read next hence, leave. This is not good. You have to keep your visitors around and have them read more articles on your website.

Getting traffic the right way, especially organic traffic is not easy. If you know this, then you’ll try to keep your visitors around for as long as you can.

Most visitors will find your website through search engines and when they visit, you’ll want to keep them longer. If your articles are interesting to read, you could actually turn them into followers.

More page views and traffic

Related posts will increase your page views and traffic. Reading more posts means having more page views and traffic. Its good to have your readers read more than 1 post on your website.

Earn more followers

Its very unlikely for a visitor to become a follower after visiting only 1 page on your website. Most gain your trust after reading a couple of informative articles.

Achieving this isn’t all about have related posts on your website. You have to consider the posts you recommend to your readers and endeavor to ensure that the topics are closely or somehow related.

A great example of this would be a visitor who visits your website through a web hosting article. They are unlikely to click on any related posts that talk about topics that are irrelevant to the reason for their visit. If you recommended more articles about the same topic, chances are high they’ll dig deeper.

Boosts your SEO

Having related posts is a great way to distribute link juice, lower bounce rate and silo your website. All these are key SEO factors that you should consider. An article with a great Page Authority (PA) is likely to help the linked pages rank higher.

Also, when visitors visit more pages on your website, your bounce rate will drop. Regarding web silos (website structures), you could pass juice to old cornerstone content through the related posts section and boost your rankings for particular keywords.

Resurface Old and Orphaned Content

Old content and orphaned content should not be neglected. These articles can still bring in more traffic. Actually, its advisable to keep updating old content. They’ve been in search results for long and in many cases, you can rank them top with just a few tweaks.

Increase Revenue

Irrespective of the ways you monetize your website, related posts will increase your revenue. More page views and traffic means more revenue for your website.

There are a lot more benefits of having related posts on your website and above are just some of them. Either way, its very vital to have them irrespective of the age of your website.

How do you manage related posts on your website? Do you display them by tag, category, randomly or creation date? I’d love to hear about it. Drop a comment below.

Authored By Kolo Pius

Kolo Pius is the founder of TutorialFAQ. He writes Badass articles for it and is an avid traveler too. Most of the time he travels while blogging and coding. You can hire me for your next Web Project, Mobile Project or SEO Services. About Me | Facebook | Twitter

0 Shares

Leave a Comment

Your email address will not be published. Required fields are marked *

More FAQs

0 Shares
Share via
Copy link