Instantly share code, notes, and snippets.
Last active
August 29, 2015 14:25
-
Save vijujohns/6d1ffef84754e82fd33a to your computer and use it in GitHub Desktop.
Related Posts without plugin http://www.wppicker.com/how-to-add-responsive-related-posts-without-plugin/
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 | |
/* | |
Outputs related posts with thumbnail -------------------------------*/ | |
function wpp_related_posts($content) { | |
if ( is_single ( ) ) { | |
global $post; | |
$numposts = 4; /*this line decides the number of related posts to include*/ | |
$count = 0; | |
$postIDs = array( $post->ID ); | |
$related = ''; | |
$tags = wp_get_post_tags( $post->ID ); | |
$cats = wp_get_post_categories( $post->ID ); | |
if ( $tags ) { | |
foreach ( $tags as $tag ) { | |
$tagID[] = $tag->term_id; | |
} | |
$args = array( | |
'tag__in' => $tagID, | |
'post__not_in' => $postIDs, | |
'showposts' => $numposts, | |
'ignore_sticky_posts' => 1, | |
'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' | |
) | |
) | |
); | |
$tag_query = new WP_Query( $args ); | |
if ( $tag_query->have_posts() ) { | |
while ( $tag_query->have_posts() ) { | |
$tag_query->the_post(); | |
/*set featured image or fallback image for related posts*/ | |
$img = has_post_thumbnail() ? get_the_post_thumbnail( $post_id, 'thumbnail' ) : '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/related.jpg" alt="' . get_the_title() . '" />'; | |
$related .= '<li><a href="' . get_permalink() . '" rel="bookmark" title="' . get_the_title() . '">' . $img . get_the_title() . '</a></li>'; | |
$postIDs[] = $post->ID; | |
$count++; | |
} | |
} | |
} | |
if ( $count <= ($numposts - 1) ) { | |
$catIDs = array( ); | |
foreach ( $cats as $cat ) { | |
$catIDs[] = $cat; | |
} | |
$showposts = $numposts - $count; | |
$args = array( | |
'category__in' => $catIDs, | |
'post__not_in' => $postIDs, | |
'showposts' => $showposts, | |
'ignore_sticky_posts' => 1, | |
'orderby' => 'rand', | |
'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' | |
) | |
) | |
); | |
$cat_query = new WP_Query( $args ); | |
if ( $cat_query->have_posts() ) { | |
while ( $cat_query->have_posts() ) { | |
$cat_query->the_post(); | |
/*set featured image or fallback image for related posts*/ | |
$img = has_post_thumbnail() ? get_the_post_thumbnail( $post_id, 'thumbnail' ) : '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/related.jpg" alt="' . get_the_title() . '" />'; | |
$related .= '<li><a href="' . get_permalink() . '" rel="bookmark" title="' . get_the_title() . '">' . $img . get_the_title() . '</a></li>'; | |
} | |
} | |
} | |
if ( $related ) { | |
$content=$content. '<div class="wpp-related-posts"><h3 class="wpp-related-title">Related Posts</h3><ul class="wpp-related-list">'.$related.'</ul></div><div class="wpp-clear"></div>'; | |
} | |
wp_reset_query(); | |
} | |
return $content ; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment