Advertisement
nlozovan

Untitled

Aug 12th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.16 KB | None | 0 0
  1. <?php
  2.  
  3. /* related posts by herarchical taxonomy */
  4. /* get tax slugs and number of similar posts */
  5. function similar_query( $post_id , $taxonomy , $nr ){
  6. if( $nr > 0 ){
  7. $topics = wp_get_post_terms( $post_id , $taxonomy );
  8.  
  9. $terms = array();
  10. if( !empty( $topics ) ){
  11. foreach ( $topics as $topic ) {
  12. $term = $topic;
  13. array_push( $terms, $term -> slug );
  14. }
  15. }
  16.  
  17. if( !empty( $terms ) ){
  18. $query = new WP_Query( array(
  19. 'post__not_in' => array( $post_id ) ,
  20. 'posts_per_page' => $nr,
  21. 'orderby' => 'rand',
  22. 'post_type' => get_post_type($post_id),
  23. 'tax_query' => array(
  24. array(
  25. 'taxonomy' => $taxonomy ,
  26. 'field' => 'slug',
  27. 'terms' => $terms ,
  28. )
  29. )
  30. ));
  31. }else{
  32. $query = array();
  33. }
  34. }else{
  35. $query = array();
  36. }
  37.  
  38. return $query;
  39. }
  40.  
  41.  
  42. $similar_posts_all = array();
  43. $symilar_criteria = options::get_value( 'blog_post' , 'similar_type' );
  44.  
  45. // foreach ($symilar_criteria as $criteria) {
  46. /* post taxonomy */
  47. $tax = $symilar_criteria;
  48.  
  49.  
  50.  
  51. /*for gallery change taxonimies values*/
  52. if(get_post_type($post -> ID) == 'gallery'){
  53. if($tax == 'post_tag'){
  54. $tax = 'gallery-tag';
  55. }elseif($tax == 'category'){
  56. $tax = 'gallery-category';
  57. }
  58. }
  59.  
  60. if(strpos($tax,'category') !== false){
  61. $categ_tax = $tax;
  62. }
  63.  
  64.  
  65. $nr = 3;
  66. $nr_columns = 4;
  67.  
  68. if($tax == 'same_author'){
  69.  
  70. $label = sprintf(__('More projects by %s','cosmotheme'), '<a href="'.get_author_posts_url($post->post_author).'">'.get_the_author_meta('display_name', $post->post_author)).'</a>';
  71.  
  72. $query = $more_from_same_author = new WP_Query( array(
  73. 'author' => $post -> post_author,
  74. 'posts_per_page' => $nr,
  75. 'post_type' => get_post_type($post -> ID),
  76. 'post__not_in' => array($post->ID)
  77. ) );
  78. }else{
  79. $label = __( 'Related Posts' , 'cosmotheme' );
  80. $query = similar_query( $post -> ID , $tax , $nr );
  81.  
  82. }
  83.  
  84.  
  85. if( !empty( $query ) ){
  86. if( $query -> found_posts < $nr ){
  87. $nr = $query -> found_posts;
  88. }
  89.  
  90. $result = $query -> posts;
  91. $similar_posts_all[] = $result;
  92. }else{
  93. $similar_posts_all[] = array();
  94. }
  95. //}
  96.  
  97.  
  98.  
  99.  
  100. /*test if there are any related posts*/
  101. $have_related = false;
  102. foreach ($similar_posts_all as $sim_posts) {
  103. if( (is_array($sim_posts) && sizeof($sim_posts)) ){
  104. $have_related = true;
  105. break;
  106. }
  107. }
  108.  
  109.  
  110.  
  111. if( !empty( $similar_posts_all) && $have_related && meta::logic( $post , 'settings' , 'related' ) && options::logic( 'blog_post' , 'show_similar' ) ){
  112.  
  113. // $symilar_criteria = options::get_value( 'blog_post' , 'similar_type' );
  114. ?>
  115. <div class="row bottom-separator">
  116. <div class="twelve columns">
  117. <div class="related-tabs">
  118. <?php
  119.  
  120.  
  121. /*get the name of the one of the category*/
  122. $categ = '';
  123. if(isset($categ_tax)){
  124.  
  125. $topics = wp_get_post_terms( $post -> ID , $categ_tax );
  126.  
  127. $terms = array();
  128. if( !empty( $topics ) ){
  129. foreach ( $topics as $topic ) {
  130. $term = $topic;
  131. $categ = $term -> name;
  132. break;
  133. }
  134. }
  135. }
  136.  
  137. $same_category_name = sprintf(__('More from %s','cosmotheme'),$categ);
  138. $same_author_name = sprintf(__('More by %s','cosmotheme'), get_the_author_meta('display_name', $post->post_author) );
  139.  
  140. $criteria_tab = array('post_tag' => __('Related posts','cosmotheme'),'category' => $same_category_name, 'same_author' => $same_author_name );
  141.  
  142. $tab_label = $criteria_tab[$symilar_criteria] ;
  143. ?>
  144. <h3 class="related-title"><span><?php echo $tab_label; ?></span></h3>
  145.  
  146. <?php
  147. ?>
  148. </div>
  149. <?php
  150. if(options::get_value( 'blog_post' , 'similar_view_type' ) == 'thumbnails_view'){
  151. $container_class = 'thumb-view';
  152. }else{
  153. $container_class = 'grid-view';
  154. }
  155. ?>
  156. <div class="row <?php echo $container_class; ?> related-posts">
  157.  
  158. <?php
  159. $index = 0;
  160.  
  161. $column_width = 'four';
  162. ?>
  163.  
  164. <?php
  165. if(isset($similar_posts_all[$index]) && is_array($similar_posts_all[$index]) && count($similar_posts_all[$index])){
  166. ?> <div id="related-<?php echo $symilar_criteria; ?>">
  167. <?php
  168. $counter = 1;
  169. foreach ($similar_posts_all[$index] as $similar) {
  170.  
  171. if(options::get_value( 'blog_post' , 'similar_view_type' ) == 'thumbnails_view'){
  172.  
  173. post::grid_view_thumbnails( $similar, $width = $column_width.' columns', $additiona_class = '', $filter_type = 'sss' );
  174. }elseif(options::get_value( 'blog_post' , 'similar_view_type' ) == 'grid_view'){
  175. post::grid_view( $similar, $width = $column_width.' columns', $additiona_class = '', $show_excerpt = true, $show_meta = true, $element_type = 'article', $is_masonry = false, $is_carousel = true );
  176. }
  177. if( ($counter % $nr_columns) == 0){
  178. echo '<div class="clear"></div>';
  179. }
  180. $counter ++;
  181. }
  182. ?>
  183. </div>
  184. <?php
  185. }
  186. ?>
  187.  
  188. <?php
  189. $index++;
  190. ?>
  191.  
  192. </div>
  193. </div>
  194. </div>
  195.  
  196.  
  197. <?php
  198.  
  199. wp_reset_postdata();
  200. }
  201. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement