Jetpack’s Related Posts module has a “large and visually striking layout” option that shows related posts at the bottom of content like this:
This tutorial provides the steps to show a similarly styled three related 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 Anthony.
We shall use CSS Grid with Flexbox fallback for arranging the related posts in columns.
Step 1
Add the following in child theme’s functions.php:
// Adds custom image size for images in Related Posts section.
add_image_size( 'related', 400, 222, true );
add_action( 'genesis_after_loop', '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 we are not on a single post page, abort.
if ( ! is_singular( 'post' ) ) {
return;
}
global $count;
$count = 0;
$related = '';
$do_not_duplicate = array();
// Get the tags for the current post.
$tags = get_the_terms( get_the_ID(), 'post_tag' );
// Get the categories for the current post.
$cats = get_the_terms( get_the_ID(), 'category' );
// If we have some tags, run the tag query.
if ( $tags ) {
$query = sk_related_tax_query( $tags, $count, 'tag' );
$related .= $query['related'];
$count = $query['count'];
}
// If we have some categories and less than 3 posts, run the cat query.
if ( $cats && $count <= 2 ) {
$query = sk_related_tax_query( $cats, $count, 'category' );
$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">Related</h3>';
echo '<div class="related-posts">' . $related . '</div>';
echo '</div>';
}
/**
* The taxonomy query.
*
* @since 1.0.0
*
* @param array $terms Array of the taxonomy's objects.
* @param int $count The number of posts.
* @param string $type The type of taxonomy, e.g: `tag` or `category`.
*
* @return string
*/
function sk_related_tax_query( $terms, $count, $type ) {
global $do_not_duplicate;
// If the current post does not have any terms of the specified taxonomy, abort.
if ( ! $terms ) {
return;
}
// Array variable to store the IDs of the posts.
// Stores the current post ID to begin with.
$post_ids = array_merge( array( get_the_ID() ), $do_not_duplicate );
$term_ids = array();
// Array variable to store the IDs of the specified taxonomy terms.
foreach ( $terms as $term ) {
$term_ids[] = $term->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',
),
);
$showposts = 3 - $count;
$args = array(
$type . '__in' => $term_ids,
'post__not_in' => $post_ids,
'showposts' => $showposts,
'ignore_sticky_posts' => 1,
'tax_query' => $tax_query,
);
$related = '';
$tax_query = new WP_Query($args);
if ( $tax_query->have_posts() ) {
while ( $tax_query->have_posts() ) {
$tax_query->the_post();
$do_not_duplicate[] = get_the_ID();
$count++;
$title = get_the_title();
$related .= '<div class="related-post">';
$related .= '<a href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . genesis_get_image(array( 'size' => 'related', 'attr' => array( 'class' => 'related-post-image' ) )) . '</a>';
$related .= '<div class="related-post-info"><a class="related-post-title" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . $title . '</a>';
$related .= '<div class="related-post-date">' . do_shortcode( '[post_date]' ) . '</div>';
$related .= '<div class="related-post-tags">' . do_shortcode( '[post_tags before="Tags: "]' ) . '</div>';
$related .= '<div class="related-post-categories">' . do_shortcode( '[post_categories before="Categories: "]' ) . '</div></div>';
$related .= '</div>';
}
}
wp_reset_postdata();
$output = array(
'related' => $related,
'count' => $count,
);
return $output;
}
Step 2
Add the following in child theme’s style.css:
.related {
background-color: #fff;
padding: 40px;
margin-bottom: 40px;
}
.related a {
text-decoration: none;
}
.related-post-image {
vertical-align: top;
}
.related-post-info {
margin-top: 10px;
font-size: 15px;
}
.related-title {
font-size: 13px;
display: inline-block;
margin-bottom: 12px;
}
.related-title:before {
content: "";
display: block;
width: 100%;
min-width: 30px;
border-top: 1px solid rgba(0,0,0,.2);
margin-bottom: 1em;
}
.related-post-date {
font-size: 14px;
color: #858585;
}
.related-post-tags,
.related-post-categories,
.related-post-tags a,
.related-post-categories a {
color: #858585;
}
.related-post-tags a:hover,
.related-post-categories a:hover {
color: #333;
}
.related-post-tags,
.related-post-categories {
font-size: 13px;
margin-top: 10px;
font-style: italic;
}
.related-posts {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
.related-posts {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
display: grid;
grid-gap: 20px;
grid-template-columns: 1fr 1fr 1fr;
}
.related-post {
width: 31.4814814815%;
opacity: 0.8;
}
.related-post:hover {
opacity: 1;
}
@supports (grid-area: auto) {
.related-post {
width: auto;
}
}
@media only screen and (max-width: 500px) {
.related-post {
width: 100%;
}
.related-posts {
grid-gap: 40px;
grid-template-columns: 1fr;
}
@supports (grid-area: auto) {
.related-post {
width: auto;
}
}
}
[…] on March 20, 2018: Follow https://sridharkatakam.com/jetpack-like-related-posts-without-jetpack-in-genesis/ […]
Two things: one, if you use the Related Posts for WP plugin instead of Jetpack, I wrote a tutorial about how to use CSS grid to create a display like this in the free version: https://www.wpfangirl.com/2017/css-grid-layout-for-related-posts-for-wordpress/
Second, it’s really interesting to see the whole function written out. Have you done any tests on how resource-intensive it is? I know there are related posts plugins that get banned from hosts like WP Engine on account of using up a lot of server resources, though I suspect that performance issues depend in part on how many posts you have.
Thanks for another great tutorial!
I have not performed any tests but from my non-ninja eyes, it looks simple as it is essentially only two additional WP queries per single post.
Thanks, Sri. Am I correct that it’s retrieving the 3 most recent posts that meet the criteria? I think that where some of these related posts plugins cause problems is that they’re doing a random order, which means they have to go through all the posts.
Yes.
Hi, If I change $showposts = 3 – $count;
to $showposts = 18 – $count;
Is that all I would need to do to show 18 related posts on each page? Or is there anything else I would need to change?
You may also need to change
to
Thank you very much
You have been very helpful
Also, what would happen if a certain category has less than 18 posts within it?
Thank you – this is a boon to non-coders like me! When I added the css code – I got the same error messages at line 77 and 102 and had no idea how to fix them. “unexpected token ‘1fr’ at line 77 col 28. “
You can safely ignore those.
The code validator built into the WordPress editor at Appearance > Editor is not up to speed with the latest in CSS.
Hi thanks for this tutorial, I’ve followed all the directions but I don’t know why the related posts are shown in vertical line instead of horizontal, could you please help me with this? Many thanks
Can you provide the URL of your site?
I am having the same issue using the News Pro theme. Thumbnails / post are stacked vertically.
Did you get a fix for this as I have applied the code and got vertical thumbnails as well?
Anybody get an update?
Hi, Sridhar! Thank you so much for this. I have one issue, though. The related posts section is displaying below the comment section. How do I move it before the comment section? Thank you!
Hi,
Change
genesis_after_loop
togenesis_before_comment_form
.Worked like a charm! Thank you so much, Sridhar!
Hi, tried this code and nothing happened??
not sure if css is cached and needs to update or something not right?
Its started working now – must be cache!
Awesome code – thanks so much
One question – is it possible to remove Tags / Categories & Date from the output and just show thumb and title
Thanks again for posting this
In the first Step’s code, replace https://pastebin.com/raw/SgH6JfWy with
$related .= '</div>';
Thanks, Sridhar, that helped me, too.
it worked beautifully, thanks. Could you tell how to make the thumbnail responsive in mobile?
I just remembered this code. Glad we got it down to a single function, much cleaner.
Going to use it in a new theme now!
Hello,
I did what you have mentioned above. But images are showing in large size and also need to remove Category and Tag which shows up in related posts.
Try regenerating thumbnails using https://wordpress.org/plugins/ajax-thumbnail-rebuild/.
Can you provide the URL of your site?
To remove category and tags from appearing in the output, comment out or delete the following:
Very helpful, thanks. Is it possible to exclude posts with certain tags from showing?
Try the following (untested):
Change
to
Replace
20
with the ID of tag you want to exclude.Source: https://wordpress.stackexchange.com/a/156257/14380
Thank you so much for this – this is exactly what I’ve been looking for.
I just have a quick question. I’d like my related posts to be the same width as the body content of a blog post. I’m not sure how to best explain it, but here’s a picture to show you what I mean: https://sweeterlittlehome.com/wp-content/uploads/2018/12/Screen-Shot-2018-12-21-at-8.00.51-am.png I’ve fiddled around in the CSS but can’t seem to figure it out. Do you know how I could do this?
Any help would be incredibly appreciated.
This is really fantastic post i have been looking for a long time! You post about Jetpack like related posts without Jetpack in Genesis Theme is very helpful for me because Jjtpack related posts sometimes really takes time to appear on the site and makes it slow to load. Your code will definitely help me a lot. I am going to try it on my one of the WordPress site where i have been using Jetpack for a long time but now i do like to remove jetpack…
Really helpful post – I’ve attempted to move the related posts to before the comment section (using genesis_before_comment_form as the hook) but while it works with the native genesis comments, it doesn’t work with Thrive Comments enabled (which I see you use on this site). Is there a different hook I could use to get the related posts before the comments with thrive enabled?
Hi there, I just tried this on Essence Pro and when I tried to use the genesis_before_comments hook, anything under the related posts section displayed in a grid (this includes comments and comment forms). I temporarily removed the grid from the css but I’d prefer to make it appear as columns. Is there something within Essence Pro that is conflicting with this code?
Hello, thanks for the tutorial, really useful. In case I want to show related pages (and not posts), how can I change this code? I tried a few things but I couldn’t manage to make it work… Thanks!
Hi ! I’m trying to get this code to work for custom post types, with custom tags and categories. I can’t seem to find how to bring in the custom taxonomy! Can anyone help out ? Thanks!
Hi,
Try the following.
In:
// Get the tags for the current post.
$tags = get_the_terms( get_the_ID(), 'post_tag' );
// Get the categories for the current post.
$cats = get_the_terms( get_the_ID(), 'category' );
replace
post_tag
andcategory
with the names of your custom taxonomies.Also, in
[post_tags before="Tags: "]
replace
post_tags
withpost_terms
.See https://my.studiopress.com/documentation/customization/shortcodes-reference/post-shortcode-reference/.
[…] members-only tutorial provides the steps to implement Jetpack-like Related Posts without Jetpack in Genesis tutorial in […]
Hi Sridhar,
Thank you for sharing this tutorial.
I used it on the Business Pro theme and it has two issues I’d like help with.
First, sometimes the related posts do not display properly in columns, you can see an example on this post: https://leadsurance.com/insurance-agency-customer-service-tips/
And here is a screenshot of how they display: https://share.getcloudapp.com/yAubqmwp
Is there some css to fix this issue?
Also, the second issue is that the same list of related posts are showing across the entire site. How can we randomize the publish dates that are selected? Currently, it is selecting the oldest posts and displaying them. Is there a way to make this random? Or to show the newest posts so that as we update the site the related posts will also update?
Thank you,
Keller
Hi Keller,
I see that each related post div has the class of
one-fourth
in your site. Do you want to display them in 4 columns or 3 columns?Regarding the same related posts appearing for every post, that could happen if there is page caching. Is that the case? Is server-side caching or a plugin-induced caching in place?