Advertisement
asadsuman

related-posts-form-current-taxonomy

Feb 8th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. function pippin_related_posts($taxonomy = '') {
  5.  
  6. global $post;
  7.  
  8. if($taxonomy == '') { $taxonomy = 'post_tag'; }
  9.  
  10. $tags = wp_get_post_terms($post->ID, $taxonomy);
  11.  
  12. if ($tags) {
  13. $first_tag = $tags[0]->term_id;
  14. $second_tag = $tags[1]->term_id;
  15. $third_tag = $tags[2]->term_id;
  16. $args = array(
  17. 'post_type' => get_post_type($post->ID),
  18. 'posts_per_page' => 4,
  19. 'tax_query' => array(
  20. 'relation' => 'OR',
  21. array(
  22. 'taxonomy' => $taxonomy,
  23. 'terms' => $second_tag,
  24. 'field' => 'id',
  25. 'operator' => 'IN',
  26. ),
  27. array(
  28. 'taxonomy' => $taxonomy,
  29. 'terms' => $first_tag,
  30. 'field' => 'id',
  31. 'operator' => 'IN',
  32. ),
  33. array(
  34. 'taxonomy' => $taxonomy,
  35. 'terms' => $third_tag,
  36. 'field' => 'id',
  37. 'operator' => 'IN',
  38. )
  39. )
  40. );
  41. $related = get_posts($args);
  42. $i = 0;
  43. if( $related ) {
  44. global $post;
  45. $temp_post = $post;
  46. foreach($related as $post) : setup_postdata($post);
  47. $content .= '<ul class="related-posts-box">';
  48. $content .= '<li><a href="' . get_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></li>';
  49. $content .= '</ul>';
  50.  
  51. endforeach;
  52. $post = $temp_post;
  53. }
  54. }
  55.  
  56. return $content;
  57. }
  58.  
  59.  
  60. add_action('the_content', 'do_jt_related_posts');
  61. function do_jt_related_posts() {
  62.  
  63.  
  64. if( is_singular('post') ) :
  65. echo get_the_content();
  66. echo pippin_related_posts();
  67. else :
  68. echo get_the_content();
  69. endif;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement