Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. <?php
  2.  
  3. function dc_related_after_content( $content )
  4. {
  5. $number_related = 4;
  6. $custom_field = 'relacionados';
  7. $counter = 0;
  8. $cad = "";
  9.  
  10. if ( !is_singular('post') ) return $content;
  11.  
  12. $template_li = '<li>
  13. <a class="thumb_rel" href="{url}">{thumb}</a>
  14. <a class="title_rel" href="{url}">{title}</a>
  15. </li>';
  16. $template_rel = '<div class="rel_posts">
  17. <h3>Artículos Relacionados</h3>
  18. <ul>
  19. {list}
  20. </ul>
  21. </div>';
  22.  
  23. // -- Entradas relacionadas específicas
  24. $related = get_post_meta( get_the_ID(), $custom_field, true );
  25. $related = array_filter(explode(',', $related), 'ctype_digit');
  26.  
  27. $loop = new WP_QUERY(array(
  28. 'post__in' => $related,
  29. 'posts_per_page' => $number_related,
  30. 'post__not_in' => array(get_the_ID()),
  31. ));
  32.  
  33. $cad = dc_loop_related($loop, $template_li, $counter);
  34.  
  35. // -- Entradas de acuerdo a la categoría
  36. if ( $number_related - $counter > 0 ){
  37. $terms = get_the_terms( get_the_ID(), 'category');
  38. $categ = array();
  39.  
  40. if ( $terms ){
  41. foreach ($terms as $term) $categ[] = $term->term_id;
  42. }
  43.  
  44. if ( count($categ) ){
  45. $loop = new WP_QUERY(array(
  46. 'category__in' => $categ,
  47. 'posts_per_page' => $number_related - $counter,
  48. 'post__not_in' =>array(get_the_ID()),
  49. 'orderby' =>'rand'
  50. ));
  51.  
  52. $cad .= dc_loop_related($loop, $template_li, $counter);
  53. }
  54. }
  55.  
  56. if ( $cad ) $content .= str_replace('{list}', $cad, $template_rel);
  57.  
  58. return $content;
  59. }
  60.  
  61.  
  62. function dc_loop_related($loop, $template_li, &$counter){
  63. $str = "";
  64.  
  65. if ( $loop->have_posts() )
  66. {
  67. while ( $loop->have_posts() )
  68. {
  69. $loop->the_post();
  70.  
  71. $search = Array('{url}','{thumb}','{title}');
  72. $replace = Array(get_permalink(),get_the_post_thumbnail(),get_the_title());
  73.  
  74. $str .= str_replace($search,$replace, $template_li);
  75. $counter++;
  76. }
  77. }
  78. wp_reset_query();
  79.  
  80. return $str;
  81. }
  82.  
  83. add_filter( 'the_content', 'dc_related_after_content');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement