Mostrar postagens por tags no Wordpress sem Plugin

 Use o código abaixo para mostrar postagens relacionadas por tags:

     <?php
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
     $tag_ids = array();
     foreach($tags as $individual_tag)
      $tag_ids[] = $individual_tag->term_id;
   
     $args=array(
      'tag__in' => $tag_ids,
      'tag__not_in' => array(31,344), //Tags a ignorar.
      'post__not_in' => array($post->ID), //Ignorar postagem da pagina.
      'showposts'=>4, // Numero de posts a mostrar.
      'caller_get_posts'=>1
     );
     $my_query = new wp_query($args);
     if( $my_query->have_posts() ) {
      echo '<div class="sua class">';
      while ($my_query->have_posts()) {
       $my_query->the_post(); ?>
      
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php if ( has_post_thumbnail() ) the_post_thumbnail( 'medium' );
         else
         echo '<img src="'.get_bloginfo('template_url').'/images/padrao-sem-imagem.png" alt="'.get_the_title().'" />';
       ?>
            <?php the_title(); ?></a>
       
      <?php }
      echo '</ul></div>';
     }
    }
   ?>


  

Postar um comentário