Instantly share code, notes, and snippets.
Created
April 28, 2016 11:24
-
Save carasmo/a6c390692b5fbf6556d65815d879d923 to your computer and use it in GitHub Desktop.
Related Posts Genesis. Requires CSS: https://gist.github.com/carasmo/42324283583bc754ddb28daa8546522f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//don't add if you're putting this at the END of your functions.php file. | |
/** | |
* | |
* Output related posts with thumbnail | |
* | |
* @author Nick the Geek / Andrea Whitmer / Chinmoy Paul / Sridhar Katakam / Christina Arasmo | |
* @link http://designsbynickthegeek.com/tutorials/related-posts-genesis | |
* @link https://sridharkatakam.com/related-posts-with-thumbnails-in-genesis-reloaded/ | |
* @global object $post | |
* | |
* Removed title attribute | |
* https://mrwweb.com/the-title-attribute-and-why-its-almost-useless/ | |
* https://silktide.com/i-thought-title-text-improved-accessibility-i-was-wrong/ | |
* | |
* Uses a background image | |
* | |
*/ | |
add_action( 'genesis_entry_footer', 'your_prefix_related_posts', 12 ); | |
function your_prefix_related_posts() { | |
global $do_not_duplicate; | |
if ( ! is_singular ( 'post' ) ) { | |
return; | |
} | |
global $related_image_size; | |
$related_image_size = 'thumbnail-400x225-cc'; //* ===== put your 16:9 image size slug here | |
$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 = your_prefix_related_tag_query( $tags, $count ); //* ===== change your_prefix here too | |
$related .= $query[ 'related' ]; | |
$count = $query[ 'count' ]; | |
} | |
//* If we have some categories and less than 6 posts, run the cat query. | |
if ( $cats && $count <= 5 ) { | |
$query = your_prefix_related_cat_query( $cats, $count ); //* ===== and change your_prefix here too | |
$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>' . __( 'You may also enjoy ...', 'your-text-domain' ) . '</h3>'; //* === change it your text-domain | |
echo '<ul>' . $related . '</ul>'; | |
echo '</div>'; | |
} | |
function your_prefix_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' => 6, | |
'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 = get_the_title(); | |
$related .= '<li>'; | |
if ( has_post_thumbnail() ) { | |
global $related_image_size; | |
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), $related_image_size ); | |
$related .= '<a style="background-image:url( ' . $image[0] . ' );" class="related-image" href="' . get_permalink() . '" rel="bookmark"></a>'; | |
} else { | |
$related .= '<a class="related-fallback" href="' . get_permalink() . '" rel="bookmark"></a>'; | |
} | |
$related .= '<h6 class="entry-title"><a href="' . get_permalink() . '" rel="bookmark">' . $title . '</a></h6>'; | |
$related .= '</li>'; | |
} | |
} | |
wp_reset_postdata(); | |
$output = array( | |
'related' => $related, | |
'count' => $count | |
); | |
return $output; | |
} | |
function your_prefix_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 = get_the_title(); | |
$related .= '<li>'; | |
if ( has_post_thumbnail() ) { | |
global $related_image_size; | |
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), $related_image_size ); | |
$related .= '<a style="background-image:url( ' . $image[0] . ' );" class="related-image" href="' . get_permalink() . '" rel="bookmark"></a>'; | |
} else { | |
$related .= '<a class="related-fallback" href="' . get_permalink() . '" rel="bookmark"></a>'; | |
} | |
$related .= '<h6 class="entry-title"><a href="' . get_permalink() . '" rel="bookmark">' . $title . '</a></h6>'; | |
$related .= '</li>'; | |
} | |
} | |
wp_reset_postdata(); | |
$output = array( | |
'related' => $related, | |
'count' => $count | |
); | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment