How to Display Related Posts in Genesis without Plugin

If you really want to engage your readers on your blog, it is a good idea to show related posts when they are reading a post. 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 Genesis theme, 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 the user experience. So, now in this tutorial, I will show you how to display related posts below your post content without any plugin.

Add 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' );

Add related post by Tags:

If you want to display related posts based on the tag, and then add this code to 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' );

The 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.

Similar Posts:

Previous Article
Recent Comments Widget with Rounded Images for Blogger
Next Article
Top Online Money Making Tips

Thanks for choosing to comment on this article. A name and email address are required to post a comment. The email address is not publicly visible or shared. Please keep in mind that comments are moderated according to our comment policy.

Leave a Comment