How to Add Related Posts in WordPress Without 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”.

$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;
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=> 15, // Number of related posts that will be shown.
'orderby'=> rand,
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div id="related_posts"><h2><u>Related Posts:</u></h2><ul><br>';
while( $my_query->have_posts() ) {
<li style="border-bottom:1px solid #FE9320; padding-bottom:5px;padding-top:8px;"><div class="relatedthumb" style="float:left;width:120px;padding-right:5px;"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php if(has_post_thumbnail()) { the_post_thumbnail(array(150,150));} else { echo '<img width="150" height="150" alt="Jai Mata Di" class="attachment-150x150 wp-post-image" src="">'; } ?></a></div>
<div class="relatedcontent"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><h3><?php the_title(); ?></h3></a>
</div><div style="clear:both"/></div>
echo '</ul></div>';
$post = $orig_post;

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

Tagged , . Bookmark the permalink.

Leave a Reply

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