Update on March 20, 2018: Follow https://sridharkatakam.com/jetpack-like-related-posts-without-jetpack-in-genesis/ instead.
Nick has published a post titled Show Related Posts with Thumbnails in Genesis in the past. Andrea Whitmer has tweaked it for her needs and kindly shared with me which then, was expertly fixed by Chinmoy for getting rid of duplicate posts.
The code below when added to child theme’s functions.php will print 5 posts related to the current entry above the comments on single posts in Genesis.
add_action( 'genesis_before_comments', 'sk_related_posts', 12 ); | |
/** | |
* Outputs related posts with thumbnail | |
* | |
* @author Nick the Geek | |
* @url http://designsbynickthegeek.com/tutorials/related-posts-genesis | |
* @global object $post | |
*/ | |
function sk_related_posts() { | |
global $do_not_duplicate; | |
if ( ! is_singular ( 'post' ) ) { | |
return; | |
} | |
$count = 0; | |
$related = ''; | |
$do_not_duplicate = array(); | |
$tags = wp_get_post_tags( get_the_ID() ); | |
$cats = wp_get_post_categories( get_the_ID() ); | |
// If we have some tags, run the tag query. | |
if ( $tags ) { | |
$query = sk_related_tag_query( $tags, $count ); | |
$related .= $query['related']; | |
$count = $query['count']; | |
} | |
// If we have some categories and less than 5 posts, run the cat query. | |
if ( $cats && $count <= 4 ) { | |
$query = sk_related_cat_query( $cats, $count ); | |
$related .= $query['related']; | |
$count = $query['count']; | |
} | |
// End here if we don't have any related posts. | |
if ( ! $related ) { | |
return; | |
} | |
// Display the related posts section. | |
echo '<div class="related">'; | |
echo '<h3 class="related-title">You might also enjoy...</h3>'; | |
echo '<div class="related-posts-list" data-columns>' . $related . '</div>'; | |
echo '</div>'; | |
} | |
function sk_related_tag_query( $tags, $count ) { | |
global $do_not_duplicate; | |
if ( ! $tags ) { | |
return; | |
} | |
$postIDs = array( get_the_ID() ); | |
foreach ( $tags as $tag ) { | |
$tagID[] = $tag->term_id; | |
} | |
$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' | |
) | |
); | |
$args = array( | |
'tag__in' => $tagID, | |
'post__not_in' => $postIDs, | |
'showposts' => 5, | |
'ignore_sticky_posts' => 1, | |
'tax_query' => $tax_query, | |
); | |
$related = ''; | |
$tag_query = new WP_Query( $args ); | |
if ( $tag_query->have_posts() ) { | |
while ( $tag_query->have_posts() ) { | |
$tag_query->the_post(); | |
$do_not_duplicate[] = get_the_ID(); | |
$count++; | |
// $title = genesis_truncate_phrase( get_the_title(), 35 ); | |
$title = get_the_title(); | |
$related .= '<div class="related-post">'; | |
$related .= '<a class="related-post-title" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . $title . '</a>'; | |
$related .= '<a class="related-image" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . genesis_get_image( array( 'size' => 'related' ) ) . '</a>'; | |
$related .= '</div>'; | |
} | |
} | |
wp_reset_postdata(); | |
$output = array( | |
'related' => $related, | |
'count' => $count | |
); | |
return $output; | |
} | |
function sk_related_cat_query( $cats, $count ) { | |
global $do_not_duplicate; | |
if ( ! $cats ) { | |
return; | |
} | |
$postIDs = array_merge( array( get_the_ID() ), $do_not_duplicate ); | |
$catIDs = array(); | |
foreach ( $cats as $cat ) { | |
if ( 3 == $cat ) { | |
continue; | |
} | |
$catIDs[] = $cat; | |
} | |
$showposts = 5 - $count; | |
$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' | |
) | |
); | |
$args = array( | |
'category__in' => $catIDs, | |
'post__not_in' => $postIDs, | |
'showposts' => $showposts, | |
'ignore_sticky_posts' => 1, | |
'orderby' => 'rand', | |
'tax_query' => $tax_query, | |
); | |
$related = ''; | |
$cat_query = new WP_Query( $args ); | |
if ( $cat_query->have_posts() ) { | |
while ( $cat_query->have_posts() ) { | |
$cat_query->the_post(); | |
$count++; | |
// $title = genesis_truncate_phrase( get_the_title(), 35 ); | |
$title = get_the_title(); | |
$related .= '<div class="related-post">'; | |
$related .= '<a class="related-post-title" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . $title . '</a>'; | |
$related .= '<a class="related-image" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . genesis_get_image( array( 'size' => 'related' ) ) . '</a>'; | |
$related .= '</div>'; | |
} | |
} | |
wp_reset_postdata(); | |
$output = array( | |
'related' => $related, | |
'count' => $count | |
); | |
return $output; | |
} |
Note that this does not include styling and other needed steps as outlined in Nick’s post.
Reference: https://gist.github.com/srikat/d62afc0443341ecbe5ea
what determines the image size?
I found what I was looking for .
. genesis_get_image( array( ‘size’ => ‘related’ ) ) . ‘‘;
change the word related to your custom size.
it’s worked for me but looking good. want to change the size of thumnail in related posts.. how to?
I’ve encountered some issues using Nick’s old code, and now yours, in combination with the Subtitles plugin. The reason why is covered here:
https://pippinsplugins.com/use-the_title-and-the_title_attribute-correctly/
When attempting to use the_title and the_title_attribute properly, what I’m seeing is the_title_attribute output above the aggregate related posts section.
Hello friend, how are you thank you for your help, I have a query like I can remove the tags and show only with categories waiting for your response.
Thank you
[…] cool but it sits nicely on mobile devices. Sridhar Katakam has already written a post on how to display related posts with thumnails in Genesis. In this post, I will basically use the same code for functions.php with some tweaking […]
Hey i tried to install it on my blog , but i am getting all oversize thumbnail images, Please tell me how can i fix it.
If you haven’t already, register a custom
related
image size and regenerate thumbnails.https://developer.wordpress.org/reference/functions/add_image_size/
Sir, i belive i dont need to register. Please go through my CSS once
https://www.codyan.com/genesis-framework/show-related-posts-thumbnails-genesis-without-plugin.html
I would like to show the excerpt of the related post along with the title and the image. Is this possible?
Thanks
Wayne
Yes.
Below
add
Ahh – thanks!
Hi,
I would like use this only for related category posts, is this possible?
yes it is
Is there an example of what the result of related posts with thumbnail going to look alike before I apply your method admin?
I will have to implement the code in a test site for that.
You might want to give it a go and see it first hand on your site.
Alternately, take a look at Jetpack’s Related Posts module. https://jetpack.com/support/related-posts/
[…] cool but it sits nicely on mobile devices. Sridhar Katakam has already written a post on how to display related posts with thumnails in Genesis. In this post, I will basically use the same code for functions.php with some tweaking […]
Hello Sir, Thank you for sharing codes with us. I have done little modification of code snippet and CSS in two ways. I hope you gotta like this to see; you can visit to:
https://www.codyan.com/genesis-framework/show-related-posts-thumbnails-genesis-without-plugin.html
Hello! Thank you so much for this code, is so useful. How can be showed the related posts in 2 or 3 columns? Thanks.
[…] posts below the comments section on single posts in Genesis based on the code by Nick Croft and improvements by Andrea and Chinmoy and a single function for category query and tag query by Lee […]
Hi there, nice feature. Thank you.
Could you please tell me, how to set a default image in case, no featured image is set? This would be great. 🙂
[…] (The code is shared from https://sridharkatakam.com/related-posts-with-thumbnails-in-genesis-reloaded) […]