How To Display Related Posts In WordPress



Allowing users to navigate throughout your site from post to post makes for longer browsing time on the site. You can display related posts at the end of the post in WordPress so your visitors more engaged with your site and you get a chance that they will convert into a lead or a sale. So in this post I will tech you how to display related posts in WordPress manually.

As we want to add our related posts functionality within each individual blog post, we need to work primarily inside our theme’s single.php file. This is the code to display related posts of same category between blog post and comments section. Open you theme’s single.php file and add following code just above the comments code like below:

// related posts
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 6, 'post__not_in' => array($post->ID) ) );

if( $related ){ ?>
<div class="related_posts">
<h2>Related Tutorials</h2>
<ul>
<?php foreach( $related as $post ) {
setup_postdata($post); ?>
	<li>
		<div class="related_post">
		<?php if ( has_post_thumbnail() ) : ?>
			<a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(150,150) ); ?></a>
		<?php endif; ?>

		<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
		</div>
	</li>
<?php } ?>
</ul>
</div>
<?php }
wp_reset_postdata();

// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
	comments_template();
}

There are so many plugin’s to display related posts based on categories, tags and taxonomies.  So you can use any of the plug-in that is suitable for your requirement.

Related posts are a great way to keep your blog engaging for your visitors. The more posts visible which are related to the blog your users are reading the better. This in return helps you and your users find the content they have been looking for. I hope this will help you to add related posts manually.