Advertisement
srikat

Untitled

Feb 25th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. <?php
  2.  
  3. add_action( 'genesis_before_comments', 'sk_related_posts', 12 );
  4. /**
  5. * Outputs related posts with thumbnail
  6. *
  7. * @author Nick the Geek
  8. * @url http://designsbynickthegeek.com/tutorials/related-posts-genesis
  9. * @global object $post
  10. */
  11. function sk_related_posts() {
  12.  
  13. global $do_not_duplicate;
  14.  
  15. if ( ! is_singular ( 'post' ) ) {
  16. return;
  17. }
  18.  
  19. $count = 0;
  20.  
  21. $related = '';
  22.  
  23. $do_not_duplicate = array();
  24.  
  25. $tags = wp_get_post_tags( get_the_ID() );
  26.  
  27. $cats = wp_get_post_categories( get_the_ID() );
  28.  
  29. // If we have some tags, run the tag query.
  30. if ( $tags ) {
  31. $query = sk_related_tag_query( $tags, $count );
  32. $related .= $query['related'];
  33. $count = $query['count'];
  34. }
  35.  
  36. // If we have some categories and less than 5 posts, run the cat query.
  37. if ( $cats && $count <= 4 ) {
  38. $query = sk_related_cat_query( $cats, $count );
  39. $related .= $query['related'];
  40. $count = $query['count'];
  41. }
  42.  
  43. // End here if we don't have any related posts.
  44. if ( ! $related ) {
  45. return;
  46. }
  47.  
  48. // Display the related posts section.
  49. echo '<div class="related">';
  50. echo '<h3 class="related-title">You might also enjoy...</h3>';
  51. echo '<div class="related-posts-list" data-columns>' . $related . '</div>';
  52. echo '</div>';
  53.  
  54. }
  55.  
  56. function sk_related_tag_query( $tags, $count ) {
  57.  
  58. global $do_not_duplicate;
  59.  
  60. if ( ! $tags ) {
  61. return;
  62. }
  63.  
  64. $postIDs = array( get_the_ID() );
  65.  
  66. foreach ( $tags as $tag ) {
  67. $tagID[] = $tag->term_id;
  68. }
  69.  
  70. $tax_query = array(
  71. array(
  72. 'taxonomy' => 'post_format',
  73. 'field' => 'slug',
  74. 'terms' => array(
  75. 'post-format-link',
  76. 'post-format-status',
  77. 'post-format-aside',
  78. 'post-format-quote'
  79. ),
  80. 'operator' => 'NOT IN'
  81. )
  82. );
  83. $args = array(
  84. 'tag__in' => $tagID,
  85. 'post__not_in' => $postIDs,
  86. 'showposts' => 5,
  87. 'ignore_sticky_posts' => 1,
  88. 'tax_query' => $tax_query,
  89. 'date_query' => array(
  90. array(
  91. 'after' => '2 year ago'
  92. )
  93. )
  94. );
  95.  
  96. $related = '';
  97.  
  98. $tag_query = new WP_Query( $args );
  99.  
  100. if ( $tag_query->have_posts() ) {
  101. while ( $tag_query->have_posts() ) {
  102. $tag_query->the_post();
  103.  
  104. $do_not_duplicate[] = get_the_ID();
  105.  
  106. $count++;
  107.  
  108. // $title = genesis_truncate_phrase( get_the_title(), 35 );
  109. $title = get_the_title();
  110.  
  111. $related .= '<div class="related-post">';
  112. $related .= '<a class="related-post-title" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . $title . '</a>';
  113. $related .= '<a class="related-image" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . genesis_get_image( array( 'size' => 'related' ) ) . '</a>';
  114. $related .= '</div>';
  115. }
  116. }
  117.  
  118. wp_reset_postdata();
  119.  
  120. $output = array(
  121. 'related' => $related,
  122. 'count' => $count
  123. );
  124.  
  125. return $output;
  126. }
  127.  
  128. function sk_related_cat_query( $cats, $count ) {
  129.  
  130. global $do_not_duplicate;
  131.  
  132. if ( ! $cats ) {
  133. return;
  134. }
  135.  
  136. $postIDs = array_merge( array( get_the_ID() ), $do_not_duplicate );
  137.  
  138. $catIDs = array();
  139.  
  140. foreach ( $cats as $cat ) {
  141. if ( 3 == $cat ) {
  142. continue;
  143. }
  144. $catIDs[] = $cat;
  145. }
  146.  
  147. $showposts = 5 - $count;
  148.  
  149. $tax_query = array(
  150. array(
  151. 'taxonomy' => 'post_format',
  152. 'field' => 'slug',
  153. 'terms' => array(
  154. 'post-format-link',
  155. 'post-format-status',
  156. 'post-format-aside',
  157. 'post-format-quote'
  158. ),
  159. 'operator' => 'NOT IN'
  160. )
  161. );
  162. $args = array(
  163. 'category__in' => $catIDs,
  164. 'post__not_in' => $postIDs,
  165. 'showposts' => $showposts,
  166. 'ignore_sticky_posts' => 1,
  167. 'orderby' => 'rand',
  168. 'tax_query' => $tax_query,
  169. 'date_query' => array(
  170. array(
  171. 'after' => '2 year ago'
  172. )
  173. )
  174. );
  175.  
  176. $related = '';
  177.  
  178. $cat_query = new WP_Query( $args );
  179.  
  180. if ( $cat_query->have_posts() ) {
  181. while ( $cat_query->have_posts() ) {
  182. $cat_query->the_post();
  183.  
  184. $count++;
  185.  
  186. // $title = genesis_truncate_phrase( get_the_title(), 35 );
  187. $title = get_the_title();
  188.  
  189. $related .= '<div class="related-post">';
  190. $related .= '<a class="related-post-title" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . $title . '</a>';
  191. $related .= '<a class="related-image" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . genesis_get_image( array( 'size' => 'related' ) ) . '</a>';
  192. $related .= '</div>';
  193.  
  194. }
  195. }
  196.  
  197. wp_reset_postdata();
  198.  
  199. $output = array(
  200. 'related' => $related,
  201. 'count' => $count
  202. );
  203.  
  204. return $output;
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement