Skip to content

Instantly share code, notes, and snippets.

@itsnahidhasan
Last active September 11, 2017 11:58
Show Gist options
  • Save itsnahidhasan/7f043d95addec95c94d35403bddd1aa0 to your computer and use it in GitHub Desktop.
Save itsnahidhasan/7f043d95addec95c94d35403bddd1aa0 to your computer and use it in GitHub Desktop.
Genesis related posts code snippets with thumbnail image
add_action( 'genesis_after_entry_content', 'sk_related_posts', 12 );
function sk_related_posts() { global $do_not_duplicate;
if ( ! is_singular ( 'post' ) ) { return; }
$count = 0; $related = ''; $do_not_duplicate = array(); $cats = wp_get_post_categories( get_the_ID() );
// 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-posts">';
echo '<h3 class="related-title">Related Posts</h3>';
echo '<div class="related-posts-list" data-columns>' . $related . '</div>';
echo '</div>';
}
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 = 3 - $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="one-third">';
$related .= '<a class="related-image" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . genesis_get_image( array( 'size' => 'related' ) ) . '</a>';
$related .= '<div class="one-copy">';
$related .= '<a class="related-post-title" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . $title . '</a>';
$related .= '</div>';
$related .= '</div>';
}
}
wp_reset_postdata();
$output = array( 'related' => $related, 'count' => $count );
return $output;
}
.related-posts {
border-top: 1px solid #ccc;
padding-top: 40px;
}
.related-posts-list .one-third {
border: 1px solid #dde5ea;
border-radius: 5px;
box-sizing: border-box;
box-shadow: 2px 1px 10px 0 rgba(0, 0, 0, .1);
}
.related-posts-list .one-third:first-child {
margin-left: 0;
}
a.related-post-title {
line-height: 27px;
color: #333;
font-size: 18px;
}
.one-copy {
padding: 1.5rem 1.25rem 1.75rem;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment