{"id":42379,"date":"2014-09-07T03:51:33","date_gmt":"2014-09-07T03:51:33","guid":{"rendered":"https:\/\/br.wordpress.org\/support\/topic\/artigos-relacionados-por-categoria-ao-inves-de-tags\/"},"modified":"2016-08-06T01:31:22","modified_gmt":"2016-08-06T01:31:22","slug":"artigos-relacionados-por-categoria-ao-inves-de-tags","status":"closed","type":"topic","link":"https:\/\/br.wordpress.org\/support\/topic\/artigos-relacionados-por-categoria-ao-inves-de-tags\/","title":{"rendered":"Artigos relacionados por categoria ao inv\u00e9s de TAGS"},"content":{"rendered":"<p><strong>Pessoal, preciso muito de uma solu\u00e7\u00e3o!<\/strong><\/p>\n<p>\u00c9 o seguinte: O tema que estou usando para um projeto (Max Mag), mostra os artigos relacionados por TAGS, e isso n\u00e3o est\u00e1 fazendo com que ele mostre REALMENTE os artigos relacionados, porque para que todos os artigos apare\u00e7am 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\u00e3o por TAGS.<\/p>\n<p>Podem me ajudar com isso?<\/p>\n<p>Abaixo segue o c\u00f3digo da fun\u00e7\u00e3o e da chamada dela dentro do single.php:<\/p>\n<p><strong>functions.php:<\/strong><\/p>\n<pre><code>function getRelatedPosts( $count=4) {\n\n    global $post;\n\n    $orig_post = $post;\n\n    $tags = wp_get_post_tags($post-&gt;ID);\n\n    if ($tags) {\n\n        $tag_ids = array();\n\n        foreach($tags as $individual_tag) $tag_ids[] = $individual_tag-&gt;term_id;\n\n        $args=array(\n\n            &#039;tag__in&#039; =&gt; $tag_ids,\n\n            &#039;post__not_in&#039; =&gt; array($post-&gt;ID),\n\n            &#039;posts_per_page&#039;=&gt; $count, \/\/ Number of related posts that will be shown.\n\n            &#039;ignore_sticky_posts&#039;=&gt;1\n\n        );\n\n        $my_query = new WP_Query( $args );\n\n        if( $my_query-&gt;have_posts() ) { ?&gt;\n\n            &lt;div id=&quot;related-posts&quot;&gt;\n\n            \t&lt;h3&gt;&lt;?php _e( &#039;Related Posts&#039;, &#039;mvp-text&#039; ); ?&gt;&lt;\/h3&gt;\n\n\t\t\t&lt;ul&gt;\n\n            \t\t&lt;?php while( $my_query-&gt;have_posts() ) { $my_query-&gt;the_post(); ?&gt;\n\n            \t\t\t&lt;li&gt;\n\n                \t\t&lt;div class=&quot;related-image&quot;&gt;\n\n\t\t\t\t\t&lt;?php if (  (function_exists(&#039;has_post_thumbnail&#039;)) &amp;&amp; (has_post_thumbnail())  ) { ?&gt;\n\n\t\t\t\t\t&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot; rel=&quot;bookmark&quot; title=&quot;&lt;?php the_title(); ?&gt;&quot;&gt;&lt;?php the_post_thumbnail(&#039;medium-thumb&#039;); ?&gt;&lt;\/a&gt;\n\n\t\t\t\t\t&lt;?php } ?&gt;\n\n\t\t\t\t\t&lt;div class=&quot;related-text&quot;&gt;\n\n\t\t\t\t\t\t&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;\/a&gt;\n\n\t\t\t\t\t&lt;\/div&gt;&lt;!--related-text--&gt;\n\n\t\t\t\t&lt;\/div&gt;&lt;!--related-image--&gt;\n\n\t\t\t\t&lt;div class=&quot;related-small&quot;&gt;\n\n\t\t\t\t\t&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot; class=&quot;main-headline&quot;&gt;&lt;?php the_title(); ?&gt;&lt;\/a&gt;\n\n\t\t\t\t&lt;\/div&gt;&lt;!--related-small--&gt;\n\n            \t\t\t&lt;\/li&gt;\n\n            \t\t&lt;?php }\n\n            echo &#039;&lt;\/ul&gt;&lt;\/div&gt;&#039;;\n\n        }\n\n    }\n\n    $post = $orig_post;\n\n    wp_reset_query();\n\n}<\/code><\/pre>\n<p><strong>single.php:<\/strong><\/p>\n<pre><code>&lt;!-- Related Posts --&gt;\n\n                        &lt;strong&gt;&lt;?php getRelatedPosts(); ?&gt;&lt;\/strong&gt;\n\n\t\t\t&lt;?php comments_template(); ?&gt;\n\n\t\t&lt;?php endwhile; endif; ?&gt;\n\n\t&lt;\/div&gt;\n\n&lt;!--post-area--&gt;\t\n\n&lt;\/div&gt;&lt;!--main --&gt;\n\n&lt;?php get_sidebar(); ?&gt;\n\n&lt;?php get_footer(); ?&gt;<\/code><\/pre>\n","protected":false},"template":"","class_list":["post-42379","topic","type-topic","status-closed","hentry","topic-tag-artigos-relacionados"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/42379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/42379\/revisions"}],"wp:attachment":[{"href":"https:\/\/br.wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=42379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}