Instantly share code, notes, and snippets.
srikat/related-posts.php Secret
Last active
June 9, 2016 20:09
-
Star
(2)
2
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save srikat/d62afc0443341ecbe5ea to your computer and use it in GitHub Desktop.
Related Posts based on Nick Croft's http://designsbynickthegeek.com/tutorials/related-posts-genesis, modified from Andrea Whitmer's https://gist.github.com/nutsandbolts/467a6bcdef875303afce by Chinmoy Paul. Live Demo.: http://thedailydoll.com/soartificiallysweet/
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 | |
/** | |
* Related Posts Methods. | |
* | |
* @package Daily Doll | |
* @subpackage Genesis | |
* @copyright Copyright (c) 2014, The Blog Maven | |
* @license GPL-2.0+ | |
* @since 2.0.0 | |
*/ | |
// Exit if accessed directly | |
if ( ! defined( 'ABSPATH' ) ) exit; | |
//* Add related posts image size | |
add_image_size( 'related', 300, 0, true ); | |
add_action( 'wp_enqueue_scripts', 'dailydoll_masonry_script' ); | |
function dailydoll_masonry_script() { | |
if ( is_singular( 'post' ) ) { | |
wp_enqueue_script( 'masonry-init', get_stylesheet_directory_uri().'/js/related-init.js', array( 'jquery-masonry' ), '1.0', true ); | |
} | |
} | |
add_action( 'genesis_before_comments', 'someday_related_posts', 12 ); | |
/** | |
* Outputs related posts with thumbnail | |
* | |
* @author Nick the Geek | |
* @url http://designsbynickthegeek.com/tutorials/related-posts-genesis | |
* @global object $post | |
*/ | |
function someday_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 = someday_related_tag_query( $tags, $count ); | |
$related .= $query['related']; | |
$count = $query['count']; | |
} | |
// If we have some categories and less than 12 posts, run the cat query. | |
if ( $cats && $count <= 11 ) { | |
$query = someday_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 someday_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' => 12, | |
'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 ); | |
$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 someday_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 ( 6 == $cat ) { | |
continue; | |
} | |
$catIDs[] = $cat; | |
} | |
$showposts = 12 - $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 ); | |
$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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment