WordPress

How to Add Related Posts in WordPress Without Plugin

Show Related post without using any wordpress plugin

For WordPress blogs lots of  free plugins are available to fulfill almost all your needs. But using tons of plugins is not so good for your side. You should avoid to use plugins blindly. There are two main reasons to avoid using lots of plugins, the first reason is your site may be slow due to loads of plugins and the second reason is you may be in trouble if the plugin developer stopped providing the support, so the outdated plugin may break your site.  So, always try to use plugin as a last option and before this you should try to achieve what do you want via changing in your theme files.

Why Related Posts listing is important?

Displaying related posts below the post being reading by user keeps busy users on reading your posts one-by-one. This increase interest of user’s reading towards your posts and its increase the user stay time on your site and reduce the site bounce rate.

How to Add Related Posts in WordPress Without Plugin?

To add related posts in same category with thumbnail  you can do this via updating scripts of single.php in your default theme folder. I am going to tell you this process step-by-step.

Log in to your wordpress site’s Admin panel and select Appearance -> Editor from menu located in left panel. Now you can see your default theme is selected here. Now click on single.php to modify this. Put below code in single.php before “endwhile; // end of the loop”.

<?php
$orig_post = $post;
global $post;
$categories = get_the_category($post-&gt;ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category-&gt;term_id;
$args=array(
'category__in' =&gt;$category_ids,
'post__not_in' =&gt; array($post-&amp;amp;amp;amp;amp;amp;amp;gt;ID),
'posts_per_page'=&gt; 15, // Number of related posts that will be shown.
'orderby'=&gt; rand,
'caller_get_posts'=&gt;1
);
$my_query = new wp_query( $args );
if( $my_query-&amp;amp;amp;amp;amp;amp;amp;gt;have_posts() ) {
echo '&amp;amp;amp;amp;amp;amp;amp;lt;div id="related_posts"&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;h2&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;u&amp;amp;amp;amp;amp;amp;amp;gt;Related Posts:&amp;amp;amp;amp;amp;amp;amp;lt;/u&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;/h2&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;br&amp;amp;amp;amp;amp;amp;amp;gt;';
while( $my_query-&amp;amp;amp;amp;amp;amp;amp;gt;have_posts() ) {
$my_query-&amp;amp;amp;amp;amp;amp;amp;gt;the_post();?&amp;amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;amp;lt;li style="border-bottom:1px solid #FE9320; padding-bottom:5px;padding-top:8px;"&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;div class="relatedthumb" style="float:left;width:120px;padding-right:5px;"&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;a href="&amp;amp;amp;amp;amp;amp;amp;lt;? the_permalink()?&amp;amp;amp;amp;amp;amp;amp;gt;" rel="bookmark" title="&amp;amp;amp;amp;amp;amp;amp;lt;?php the_title(); ?&amp;amp;amp;amp;amp;amp;amp;gt;"&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;?php if(has_post_thumbnail()) { the_post_thumbnail(array(150,150));} else { echo '&amp;amp;amp;amp;amp;amp;amp;lt;img width="150" height="150" alt="Jai Mata Di" class="attachment-150x150 wp-post-image" src="http://yourdomainname.com/default-image-path.gif"&amp;amp;amp;amp;amp;amp;amp;gt;'; } ?&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;amp;lt;div class="relatedcontent"&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;a href="&amp;amp;amp;amp;amp;amp;amp;lt;? the_permalink()?&amp;amp;amp;amp;amp;amp;amp;gt;" rel="bookmark" title="&amp;amp;amp;amp;amp;amp;amp;lt;?php the_title(); ?&amp;amp;amp;amp;amp;amp;amp;gt;"&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;h3&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;?php the_title(); ?&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;/h3&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;div style="clear:both"/&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;amp;lt;?
}
echo '&amp;amp;amp;amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;amp;gt;';
}
}
$post = $orig_post;
wp_reset_query();
?&amp;amp;amp;amp;amp;amp;amp;gt;

The related post out put will display as like below:
How to Add Related Posts in WordPress Without Plugin

Try this code and let me know if you face any issue. Please also give your suggestions via comments to make this better. Thanks

Comments (1)

  1. This algorithm would find other posts within the same category as the current post, and it will list them as related posts. The advantage of this technique is that you will never have a blank spot for your related posts section.

Comment here