Title: Artigos relacionados por categoria ao invés de TAGS
Last modified: 6 agosto, 2016

---

# Artigos relacionados por categoria ao invés de TAGS

 *  [lpeixoto](https://br.wordpress.org/support/users/lpeixoto/)
 * (@lpeixoto)
 * [12 anos, 1 mês atrás](https://br.wordpress.org/support/topic/artigos-relacionados-por-categoria-ao-inves-de-tags/)
 * **Pessoal, preciso muito de uma solução!**
 * É o seguinte: O tema que estou usando para um projeto (Max Mag), mostra os artigos
   relacionados por TAGS, e isso não está fazendo com que ele mostre REALMENTE os
   artigos relacionados, porque para que todos os artigos apareçam no slider principal,
   preciso adicionar uma TAG em comum em todos eles. Por este motivo preciso que
   os artigos se relacionem por categorias e não por TAGS.
 * Podem me ajudar com isso?
 * Abaixo segue o código da função e da chamada dela dentro do single.php:
 * **functions.php:**
 *     ```
       function getRelatedPosts( $count=4) {
   
           global $post;
   
           $orig_post = $post;
   
           $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,
   
                   'post__not_in' => array($post->ID),
   
                   'posts_per_page'=> $count, // Number of related posts that will be shown.
   
                   'ignore_sticky_posts'=>1
   
               );
   
               $my_query = new WP_Query( $args );
   
               if( $my_query->have_posts() ) { ?>
   
                   <div id="related-posts">
   
                   	<h3><?php _e( 'Related Posts', 'mvp-text' ); ?></h3>
   
       			<ul>
   
                   		<?php while( $my_query->have_posts() ) { $my_query->the_post(); ?>
   
                   			<li>
   
                       		<div class="related-image">
   
       					<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
   
       					<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail('medium-thumb'); ?></a>
   
       					<?php } ?>
   
       					<div class="related-text">
   
       						<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
   
       					</div><!--related-text-->
   
       				</div><!--related-image-->
   
       				<div class="related-small">
   
       					<a href="<?php the_permalink() ?>" class="main-headline"><?php the_title(); ?></a>
   
       				</div><!--related-small-->
   
                   			</li>
   
                   		<?php }
   
                   echo '</ul></div>';
   
               }
   
           }
   
           $post = $orig_post;
   
           wp_reset_query();
   
       }
       ```
   
 * **single.php:**
 *     ```
       <!-- Related Posts -->
   
                               <strong><?php getRelatedPosts(); ?></strong>
   
       			<?php comments_template(); ?>
   
       		<?php endwhile; endif; ?>
   
       	</div>
   
       <!--post-area-->	
   
       </div><!--main -->
   
       <?php get_sidebar(); ?>
   
       <?php get_footer(); ?>
       ```
   

Visualizando 1 resposta (de um total de 1)

 *  Moderador [Allyson](https://br.wordpress.org/support/users/allysonsouza/)
 * (@allysonsouza)
 * [12 anos atrás](https://br.wordpress.org/support/topic/artigos-relacionados-por-categoria-ao-inves-de-tags/#post-42725)
 * Olá lpeixoto,
 * Na sua função getRelatedPosts() você deve alterar a busca por tags, com wp_get_post_tags,
   por wp_get_post_categories, e na query alterar o tags__in por category__in, além
   de possíveis outros detalhes.
 * Recomendo o estudo da página:
    [http://codex.wordpress.org/Class_Reference/WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query)
 * E da função:
    wp_get_post_categories
 * Espero que ajude!

Visualizando 1 resposta (de um total de 1)

 O tópico ‘Artigos relacionados por categoria ao invés de TAGS’ está fechado para
novas respostas.

## Tags

 * [artigos relacionados](https://br.wordpress.org/support/topic-tag/artigos-relacionados/)

 * Em: [Temas](https://br.wordpress.org/support/forum/temas-e-modelos/)
 * 1 resposta
 * 2 participantes
 * Última resposta de: [Allyson](https://br.wordpress.org/support/users/allysonsouza/)
 * Última atividade: [12 anos atrás](https://br.wordpress.org/support/topic/artigos-relacionados-por-categoria-ao-inves-de-tags/#post-42725)
 * Estado: não resolvido

## Tópicos

### Tópicos mais populares

### Tópicos sem respostas

### Tópicos não relacionados a suporte

### Tópicos resolvidos

### Tópicos não resolvidos

### Todos os tópicos
