Advertisement
miriamdepaula

WordPress: Posts Relacionados by Tags

Apr 1st, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.66 KB | None | 0 0
  1. <?php
  2. //for use in the loop, list 5 post titles related to tags on current post
  3. function related_posts_by_tags(){
  4.    
  5.     global $post;
  6.    
  7.     $tags = wp_get_post_tags($post->ID);
  8.     if ($tags) {
  9.        
  10.         echo '<h3 class="posts_relacionados">Se você gostou deste artigo, também vai se interessar por:</h3>';
  11.        
  12.         $t = array();
  13.         foreach($tags as $tag){
  14.             $t[] = $tag->term_id;  
  15.         }
  16.        
  17.         $args = array(
  18.             'tag__in' => $t,
  19.             'post__not_in' => array($post->ID),
  20.             'posts_per_page' => 5,
  21.             'caller_get_posts' => 1,
  22.             'post_type' => get_post_type()
  23.         );     
  24.         $q = new WP_Query($args);
  25.        
  26.         /***** LISTA DAS IMAGENS DESTACADAS DOS POSTS RELACIONADOS *****/
  27.         if( $q->have_posts() ) {
  28.             echo '<ul class="galeria_posts_relacionados">';
  29.             while ($q->have_posts()) : $q->the_post();
  30.                 echo '<li><a href="'. get_permalink() .'" rel="bookmark" title="'. the_title_attribute('echo=0'). '">'. get_the_post_thumbnail() .'</a></li>';
  31.        
  32.             endwhile;
  33.             echo '</ul>';
  34.         }
  35.         /***** LISTA DAS IMAGENS DESTACADAS DOS POSTS RELACIONADOS *****/
  36.        
  37.         /***** LISTA DOS TÍTULOS DOS POSTS RELACIONADOS *****/
  38.         if( $q->have_posts() ) {
  39.             echo '<ul class="posts_relacionados">';
  40.             while ($q->have_posts()) : $q->the_post();
  41.                 echo '<li><a href="'. get_permalink() .'" rel="bookmark" title="'. the_title_attribute('echo=0'). '">'. get_the_title() .'</a></li>';
  42.        
  43.             endwhile;
  44.             echo '</ul>';
  45.         }
  46.         /***** LISTA DOS TÍTULOS DOS POSTS RELACIONADOS *****/
  47.  
  48.         wp_reset_query();
  49.     }
  50. }
  51.  
  52. /******************
  53. USAGE/MODO DE USAR:
  54. In page template, inside the loop, put this: related_posts_by_tags();
  55. ****************************************************/
  56. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement