An Exhaustive Guide on Displaying Related Posts Using Categories


Retaining visitors’ attention is something that can’t be achieved overnight. To keep visitors stuck to your posts/pages, you need to ensure seamless navigation throughout the website. In-line with this fact, if you’re operating a WordPress powered blog then reducing the browsing time will serve as the result-oriented strategy for boosting the overall readership for your articles/posts.

Tag-based display of related posts VS category-based display of related posts- Understand the difference.

An appropriate display of related blogs/articles is what you need to look into well for affirming maximum engagement on your WordPress site/blog. As a strong alternative to tag-based display of related posts, displaying the posts as per category of current blog post can be implemented easily, without the use of any plugins. Unlike categories, tags aren’t been used by everyone. Especially, the websites built with a clear aim include a specific set of categories which serve as the hub for relevant blog posts.

What all you’ll learn through this tutorial?

This is a tutorial that will help you in understanding the key advantages of showcasing related blog posts using the categories method. Moreover, I’ll also be discussing the way in which you can display all the related blog posts using categories.

To start off, let me walk you through some prominent benefits of using post categories method for exhibiting related blog posts on a page:

  1. As the very first advantage of using the categories method, you’ll never have your readers complaining about the display of irrelevant blog posts. Since the posts are showcased in accordance to the category of the current post; the chances of presenting un-related blog posts become zinch.
  2. Categories allow broader grouping of blog post topics. To put it simply, by choosing the categories method for displaying related posts, you can render your visitors an amazing flexibility of picking an article/blog from a wider collection of articles which are fetched from the same category; the currently viewed article belongs to.

And now, let’s understand the single step that marks the display of related blog posts using the categories method.

To ensure that all related blogs/articles for a particular post are displayed using categories, you simply need to copy and paste the below mentioned code in your WordPress blog’s single.php file:

	$cat = get_the_category( $post->ID );
	foreach($cat as $cd){
		$cat_id =  $cd->term_id;
	$args = array(
		'posts_per_page' => 5,
		'cat'            => $cat_id,
		'post__not_in'   => array($post->ID),

	$posts = new WP_Query( $args );
	echo '<h2>Related Posts</h2><br/>';
	echo '<ul>';
	while($posts->have_posts()) : $posts->the_post();
	echo '<li>'.get_the_title().'</li><br/><br/>';
	endwhile; wp_reset_query();
	echo '</ul>';

Place the above code anywhere in your single.php file and it will work in the desired way. On observing the above mentioned code, you’ll be able to detect the following:

  1. $cat = get_the_category( $post->ID)- This line of code includes the get_the_category( $post-> ID which fetches the current post category in the form of an array and stores the same in $cat variable.
  2. foreach($cat as $cd){ $cat_id = $cd->term_id; }- This is a PHP loop which interacts with the array(the one which contains the current post category) and extracts the category id.
  3. $posts = new WP_Query( $args );- This is a class which is being used for displaying all the remaining blog posts that are available in the category(here, I’m referring to the category; the current post belongs to). In other words, all the posts available in the respective category, except the one currently displayed on the screen; would be visible.

Apart from above, do make a note of the UI and LI HTML tags that have been used for displaying related blog posts in a sequence as 1,2,3,,,,and so on. If you feel the need to represent the related posts in a different format, you can always opt for using a different pair of HTML tags.

So with that you’re done with displaying blog posts(belonging to the same category as that of the current post) within your WordPress website’s ‘Related Posts’ section.

Below is the screen-shot for how the related posts section would be visible once you’re done with incorporating the above mentioned code in your single.php file:


A wide variety of codes available over the internet can’t actually be used within the main post loop. It is here that the code(the one explained above) serves as a handy option for ensuring desired positioning of the related blog posts. Despite the format you’re intending to choose for showcasing the related blog posts, using the categories method will enable you to add that extra tint of elegance to the ‘Related Posts’ area within your WordPress site.

Final Words

Considering the amount of significance that’s attached to a website’s ‘Related Posts’ section, I’m sure the above mentioned guide would’ve offered a clearer picture about displaying the related articles/blogs in the right way. Right from enhancing readership for your blog to helping you in improving the website conversion rate; the related blog posts section will serve as a handy tool.

An Exhaustive Guide on Displaying Related Posts Using Categories
6 votes, 2.17 avg. rating (48% score)
  • Posted in: PHP, Tutorials, WordPress

Posted by

Emily is a professional WordPress developer for a leading company WordPrax Ltd. She also provides the services like HTML to WordPress theme conversion and many more. She has served many WordPress companies helping them in developing user-friendly website. So feel free to contact her.