add related posts to wordpress without plugin

[How To] Add Related Posts To WordPress Without Plugin

The use of plugins on a WordPress blog in achieving virtually anything is what makes WordPress blogging platform more fun as a CMS. But as the saying goes “Too much of everything is bad“. The use of too many plugins can have a negative impact on a blog.

When I say it could affect a blog, it could be in terms of loading time or otherwise hence its highly advisable to go the manual way(codes) for anything that can easily be achieved manually.

Before now, I used to use plugins in adding related posts on a blog. I used to use contextual related post and nRelate but I had decided to go the manual way in adding related post to techyan blog.

Why Manual Way In Adding Related Post To WordPress?

Please do not get me wrong. Using those plugins did not pose any negative impact(at least, none that I know of) but I’d think its safer to reduce the number of plugins I have on my blog thus I initially choose to using manual codes since it can be achieved.

Related: [How To] Install And Use Carrington Mobile Theme On WordPress

Steps To Adding Related Post To WordPress Blog With Thumbnails Without Plugin

Please Note: This method in adding related post to your WordPress blog uses tags in displaying posts that are related.


add related posts to wordpress without plugin

As you probably know, WordPress features a built-in thumbnail system, which we will also be needing here. In order to enable it, all you need to do is to add the following code to your functions.php file in your theme folder.

  1. add_theme_support( ‘post-thumbnails’ );
  2. set_post_thumbnail_size( 150, 150, true );

Save your functions.php file. After that, navigate to where you would like to have the related posts displayed and paste the following code. Preferably, I’d advice below your blog post content, hence you will need to navigate to your theme’s single.php file and post the code where you would want it displayed.

Here’s the code:

  1. <div class=“relatedposts”>
  2. <h3>Related posts</h3>
  3. <?php
  4.     $orig_post = $post;
  5.     global $post;
  6.     $tags = wp_get_post_tags($post->ID);
  7.     if ($tags) {
  8.     $tag_ids = array();
  9.     foreach($tags as $individual_tag$tag_ids[] = $individual_tag->term_id;
  10.     $args=array(
  11.     ‘tag__in’ => $tag_ids,
  12.     ‘post__not_in’ => array($post->ID),
  13.     ‘posts_per_page’=>4, // Number of related posts to display.
  14.     ‘caller_get_posts’=>1
  15.     );
  16.     $my_query = new wp_query( $args );
  17.     while$my_query->have_posts() ) {
  18.     $my_query->the_post();
  19.     ?>
  20.     <div class=“relatedthumb”>
  21.         <a rel=“external” href=“<? the_permalink()?>”><?php the_post_thumbnail(array(150,150)); ?><br />
  22.         <?php the_title(); ?>
  23.         </a>
  24.     </div>
  25.     <? }
  26.     }
  27.     $post = $orig_post;
  28.     wp_reset_query();
  29.     ?>
  30. </div>

Pros: Reduces the right of plugin issues.

Cons: Uses tags in displaying related posts, hence if your posts are not tagged using same tags for multiple posts, there might not be any related posts to be reduced beneath some post content.

There you have it pal. Following the above steps, you can easily add related posts to WordPress blog without the use of plugin for that function.

About Adeniyi Samuel

Hello! I am Samuel Adeniyi. The guy behind most tutorials on this very blog. I love experimenting with devices and for every device i experiment with, i usually write something about it here for my esteemed readers. You can follow me on twitter via @iamsammyadeniyi

kindly Leave a Reply