Display Related Posts in Genesis Without Any Plugin

If you really want to engage your readers on your blog, it is a good idea to show related posts/articles when they are reading a post/article. By using related posts to display similar content, the visitors of your website might be interested in another article that has been displayed there. This means they stay more on your website, which reduces the bounce rate of your blog.

Actually showing related posts is pretty easy and there are many plugins available to display Related Posts in the Genesis framework, but it is not good to depend on plugins all the time. You know that adding every plugin to your WordPress blog will slow down your blog even more and in my opinion, you should only have the bare minimum plugins. So, if you are using the Genesis theme, all you need to do is copy and paste the following code into your functions.php file. You can show related posts based on tags or categories. This method also improved the total page loading and improves user experience. So, let's dig deeper into the tutorial on how to show/display related posts below your post content without any plugin.

How to add/Display Related Posts in Genesis

Display Related Post by Category:

If you want to display related posts based on the category, and then add this code to functions.php file in Genesis Child Theme and save it.

/** Display related posts in Genesis based on Category  */
function related_posts_categories() {
if ( is_single ( ) ) {
global $post;
$count = 0;
$postIDs = array( $post->ID );
$related = '';
$cats = wp_get_post_categories( $post->ID );
$catIDs = array( );{
foreach ( $cats as $cat ) {
$catIDs[] = $cat;
}
$args = array(
'category__in'          => $catIDs,
'post__not_in'          => $postIDs,
'showposts'             => 5,
'ignore_sticky_posts'   => 1,
'orderby'               => 'rand',
'tax_query'             => array(
array(
'taxonomy'  => 'post_format',
'field'     => 'slug',
'terms'     => array(
'post-format-link',
'post-format-status',
'post-format-aside',
'post-format-quote' ),
'operator' => 'NOT IN'
)
)
);
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) {
while ( $cat_query->have_posts() ) {
$cat_query->the_post();
$related .= '<li><a href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to' . get_the_title() . '">' . get_the_title() . '</a></li>';
}
}
}
if ( $related ) {
printf( '<div><h3>Related Posts</h3><ul>%s</ul></div>', $related );
}
wp_reset_query();
}
}
add_action( 'genesis_after_post_content', 'related_posts_categories' );

Display Related Post by Tags:

If you want to display related posts based on the tag, and then add this code to the functions.php file in Genesis Child Theme and save it.

/** Display related posts in Genesis based on Tags */
function related_posts_tags () {
if ( is_single ( ) ) {
global $post;
$count = 0;
$postIDs = array( $post->ID );
$related = '';
$tags = wp_get_post_tags( $post->ID );
foreach ( $tags as $tag ) {
$tagID[] = $tag->term_id;
}
$args = array(
'tag__in'               => $tagID,
'post__not_in'          => $postIDs,
'showposts'             => 5,
'ignore_sticky_posts'   => 1,
'tax_query'             => array(
array(
'taxonomy'  => 'post_format',
'field'     => 'slug',
'terms'     => array(
'post-format-link',
'post-format-status',
'post-format-aside',
'post-format-quote'
),
'operator'  => 'NOT IN'
)
)
);
$tag_query = new WP_Query( $args );
if ( $tag_query->have_posts() ) {
while ( $tag_query->have_posts() ) {
$tag_query->the_post();
$related .= '<li><a href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to' . get_the_title() . '">' . get_the_title() . '</a></li>';
$postIDs[] = $post->ID;
$count++;
}
}
if ( $related ) {
printf( '<div><h3>Related Posts</h3><ul>%s</ul></div>', $related );
}
wp_reset_query();
}
}
add_action( 'genesis_after_post_content', 'related_posts_tags' ); Functionality of this code:
  • Displays related posts randomly based on a tag on a single post page.
  • Define the number of posts to display by changing the value in ‘showposts' => 5 Now, you are done with everything and it's time to check the related posts in your blog. If you got any queries regarding this article, please drop a comment and we will get back to you soon.

Leave a Comment

Previous Article
Further Optimize the Performance of Your Site Using a CDN
Next Article
Tips To Increase The Readability Of Your Blog