Guest User

related posts / tri.be calendar events

a guest
May 14th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.76 KB | None | 0 0
  1. function rm_related_posts($post_id) {
  2.    
  3.     $tags = wp_get_post_tags($post_id);
  4.    
  5.     if (!$tags)
  6.         return false;
  7.    
  8.     // creo un array con i tag di questo articolo
  9.     $tag_ids = array();
  10.     foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
  11.  
  12.     $args_related =array(
  13.         'post_type' => 'post',
  14.         'tag__in' => $tag_ids,
  15.         'post__not_in' => array($post_id),
  16.         'posts_per_page'=> 6, // Number of related posts to display.
  17.     );
  18.  
  19.     $rm_related = new WP_Query($args_related);
  20.    
  21. //    echo '<pre>';
  22. //    print_r($rm_related);
  23. //    echo '</pre>';
  24.    
  25.     if ($rm_related->have_posts()) {
  26.  
  27.         // mostro titoletto
  28.         ?>
  29.  
  30.         <div class="row row-related-posts">
  31.             <div class="col-md-12">
  32.                 <h3><?php _e('Ti potrebbe interessare'); ?></h3>
  33.             </div>
  34.         </div>
  35.  
  36.         <?php
  37.  
  38.         $i=0;
  39.  
  40.         while ($rm_related->have_posts()) : $rm_related->the_post();
  41.        
  42.             if ($i==0)
  43.                 echo '<div class="row">';
  44.  
  45.             ?>
  46.    
  47.             <div class="col-md-4">
  48.                 <a rel="external" href="<? the_permalink()?>"><?php rm_post_thumbnail(); ?></a>
  49.                 <h2><a rel="external" href="<? the_permalink()?>"><?php the_title(); ?></a></h2>
  50.             </div>
  51.  
  52.             <?php
  53.            
  54.             if ($i<2) {
  55.                 $i++;
  56.             } else {
  57.                 // รจ il post "dispari"
  58.                 $i=0;
  59.                 echo '</div>';
  60.             }
  61.                            
  62.         endwhile;
  63.        
  64.         // fix
  65.         // chiudo div eventualmente lasciato aperto...
  66.         if ($i==1)
  67.             echo '</div>';
  68.  
  69.     }
  70.  
  71.     wp_reset_postdata();
  72.    
  73. }
Advertisement
Add Comment
Please, Sign In to add comment