Display Related Posts by Category

This document is deprecated! The information on this page refers to a Thesis version that is now obsolete. Please visit the Thesis Docs for current documentation.

When visitors finish reading one of your posts, it is important to provide a clear “what next?” option; in the absence of cues to keep interacting with your site, visitors are more likely to leave.  You need a call to action!

Providing “related posts” links below your individual posts improves your visitor experience — and increases your visitors’ time on site, their average number of page views, and in doing so, sends positive signals to search engines. This helps boost your site’s ranking and authority over time.

A number of plugins exist that generate “related posts” on your site. The majority of these rely on tags as an indication of relevance. While tags can be effective for relevancy, not everyone uses them – or should. Sites built with a clear purpose have categories that contain inherently relevant posts — and, unlike tags, nearly everyone uses categories.

As a result, returning a handful of related posts from the same category is a strong alternative to tag-based relational posts.

What’s more, implementing category-based “relevant posts” is easy to do, and requires no plugins. Simple add the following code to custom_functions.php:

function my_related_posts() {
if (is_single()) {
	global $post;
	$current_post = $post->ID;
	$categories = get_the_category();
foreach ($categories as $category) :
	?>
<div class="my-recent-posts"><h4>Additional Reading...</h4><ul>
	<?php
	$posts = get_posts('numberposts=5&category='. $category->term_id . '&exclude=' . $current_post);
foreach($posts as $post) :
	?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
	<?php endforeach; ?><?php endforeach; ?>
</ul>
</div>
	<?php
		}
	wp_reset_query();
}
add_action('thesis_hook_after_post','my_related_posts');

NOTE: to keep this simple, the above function presumes “one post, one category” as the assignation model. If you assign your posts to multiple categories, “related” posts will appear from each category to which the single post is assigned.

Above, the use of the condition statement is_single() restricts output to single posts.

In the function contents, you have the ability to modify the “Additional Reading” text to verbiage of your choice.

The 5 in numberposts=5 can be modified to show more or fewer posts from the same category.

Naturally, you’ll want to make this section look nice! To style your category-based related posts, you’ll need to make edits with CSS.  The below provides matching selectors and value placeholders that can be adjusted to match your design.

In custom.css add the following:

.custom .my-recent-posts { border:1px dotted #666; padding:1.0em; margin-bottom:1.8em; }
	.custom .my-recent-posts h4 { margin-bottom:1.0em; }
	.custom .my-recent-posts a { color:#cc0000; border-bottom:1px dotted #fff; text-decoration:none; }
		.custom .my-recent-posts a:hover { border-bottom:1px dotted #cc0000; }

The custom_functions.php and custom.css combine to look like this:

Sample Related Posts by Category

There you have it.  Now, each post on your site ends with a section of same-category related posts — encouraging your visitors to read more of your content!