How to display related posts in Genesis Theme without a Plugin

This tutorial is exclusively for Genesis Framework, the best WordPress theme you will ever need. Give it a try and stay ahead of the Crowd. Period.

Share This Article:

Many plugins are available to display Related Posts in Genesis theme, but it is not good to depend on plugins all the time. Hence, follow this simple tutorial to display related posts in Genesis theme without a plugin.

dislay-related-posts-in-genesis-without-plugin

You need to edit these files in your Genesis Child theme folder

functions.php
style.css

As you are going to edit some files, is advised to backup your child theme.

Also Read : How to Remove or Change Post Meta in Genesis Theme Framework

Related posts can be displayed based on tags or categories. Nick used both tags and categories in his code to display related posts, but we tried to split them and here is the result of it.

Display Related Posts in Genesis based on Category

If you want to display related posts based on the category, 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' );

Functionality of this code :

  • Displays related posts randomly based on category in single post page.
  • Define the number of posts to display by changing value in 'showposts' => 5

Also Check : 5 Reasons why we choose Genesis over Thesis

Display Related Posts in Genesis based on Tags

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

Functionality of this code :

  • Displays related posts randomly based on tag in single post page.
  • Define the number of posts to display by changing value in 'showposts' => 5

Check This : How to add 3 column footer to WordPress theme

Styling the related posts in Genesis Theme

You need to add this code to style.css file.

.related-posts {
margin: 10px 0;
}
.related-posts h3 {
font-size: 18px;
}
.related-posts ul {
list-style:none;
}
.related-posts ul li {
padding: 3px 0;
border-bottom: 1px dashed #ccc;
}
.related-posts ul li a{
font-size:14px;
text-decoration:none;
}

Now, you are done with everything and its time check the related posts in your blog. If you got any query regarding this article, please drop a comment and we will get back to you soon.

This tutorial is exclusively for Genesis Framework, the best WordPress theme you will ever need. Give it a try and stay ahead of the Crowd. Period.

Genesis is the most flexible WordPress theme framework that comes with tons of options. We have been using Genesis on all of our blogs and recommending the same to you. Every bit of it is coded according to the standards. Give it a try!

Comments

  1. Plaban says:

    Is it possible to show thumbnails in related posts?

    • WPSquare says:

      Yeah, its possible and we will write an article on that, very soon :)

  2. Ammar Ali says:

    Will this work for Thesis users too ??

    • WPSquare says:

      This code works for Thesis Theme also, but you need to replace Genesis hooks with Thesis hooks, hope you understand it.

  3. deza says:

    Could you please tell me how to get related post as thumbnails, and how to get social icons (like yours, to the top right corner “stay connected widget”)

    Thanks n advance

    • WPSquare says:

      Yeah sure, will write an article on “Related Posts with Thumbnails” and “Social Icons Widget” for you !

  4. Chris Lynn says:

    Hi,

    One thing out of context, Not sure you will tell me or not.
    How to have a Logo behind code like yours.If you are not interested in sharing your secret at least give me some reference for that.

    Thanks,

  5. Nikhil says:

    Thanks for this!

    Do I just have to Copy Paste the code into the stylesheet or any other modifications are to be made before that? Because I cannot find any different with and without the stylesheet code.

    • nikhil says:

      I am not sure about where to enter the code exactly in the stylesheet. Can you brief me on this?

  6. CTN says:

    Nice. Thank you! Yes, it would be really nice with thumbnails!

  7. Amit says:

    How to display it in between ? i mean say after 200 words ?

  8. Chris M. says:

    Hello there!
    Thanks for the great tutorial. Do you have a sample of what the output actually looks like?
    Thanks again…

    • Chris Moore says:

      I already answered my last question for myself. I just went and did it! :-)
      I tried the code that you shared above (for the related posts by category) in my Genesis child theme’s functions.php and it works perfectly. However, I had to add the CSS class “related-posts” to the “div” that you that you created, otherwise the CSS wouldn’t pick up on it.

      Finally, could you tell me how to convert the “related posts by category” code to a plug-in? I had trouble with some of the other plugins out there. I heard there was some kind of speed/memory benefit by putting this code in a plugin vs. putting it in the functions.php.

      Your thoughts either way?

      Thanks.

  9. Rednasil says:

    Thanks a lot for this tutorial.. I really needed it.
    But when are you getting to make one with thumbnails??

  10. Stacey says:

    Perfect. Just what I needed!

Speak Your MindComments Policy →

*

Looking for More Awesomeness?

Read previous post:
customize 404 Error Page in Thesis Theme
How to customize 404 Error Page in Thesis Theme

Thesis is one of the most popular WordPress theme frameworks used by bloggers and webmasters. Each and every part of...

Close