|
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; |
|
|
|
} |