Display related post in WordPress without plugin

There are a lot of ways to increase the number of people who visit your website.

But the easiest of all is to display related posts at the end of every post.

This will not only reduce your bounce rate as people might click through to read another post or two but will also increase the time people spend on your website. Both of which are a quality signal for Google. And of course, it will increase your page views.

There are two methods available to show related posts. The first method is via using any good plugin and the second method is to do it manually. Here we’ll show you how you can show related posts manually.

To add the functionality manually, you will have to add the following code to your wordpress theme wherever you want to show your related post. Usually, we show related posts at the end of the post.


<div class="related-posts-after-content">
  <h3>You Might Also Like</h3>
  <?php
    $orig_post = $post;
    global $post;
    $tags = wp_get_post_tags($post->ID);
    if($tags){
      $tag_ids = array();
      foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
        $args = array('tag__in'=>$tag_ids,'post__not_in'=>array($post->ID),
        'posts_per_page'=>4,'caller_get_posts'=>1);
        $my_query = new wp_query($args);
        while($my_query->have_posts()){
          $my_query->the_post(); ?>
          <div class="related-thumb">
            <a rel="external" href="<?php the_permalink(); ?>">
              <?php the_post_thumbnail(array(150,100)); ?><br />
              <?php the_title(); ?>
            </a>
          </div>
        <?php }
     }
     $post = $orig_post;
     wp_reset_query();?>
</div>

 

Read also: Facts you need to know about WordPress

Read also: Thrive Themes – WordPress themes and plugins

Comments

Leave a Reply

Your email address will not be published.