Existe uma maneira de exibir no WordPress os posts relacionados sem precisar instalar um plugin. A vantagem 茅 que voc锚 pode editar da maneiro que quiser a estrutura de como vai ser exibida esses posts relacionados.
Voc锚 pode exibir esses posts relacionados pelas tags ou pela categorias. Ou seja, posts que tiverem as mesmas tags/categorias ir茫o aparecer nessa lista de posts relacionados.
Exibir os post relacionados pelas tags
Abra o arquivo single.php por exemplo, e nele adicione o c贸digo dentro do if do post:
<?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,
'post__not_in' => array($post->ID),
'showposts'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Related Posts</h3><ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
}
?>
Exibir os post relacionados pelas categorias
Abra o arquivo single.php por exemplo, e nele adicione o c贸digo dentro do if do post:
<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'showposts'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Related Posts</h3><ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
}
?>
Ol谩! faz algum tempo que procuro esta forma de exibi莽茫o de artigos relacioandos (cotegoria) sem a necessidade de usar um plugin. Usei o c贸digo php que vc informou e funcionou muito bem. Mas notei que com o c贸digo na p谩gina single.php houve uma mudan莽a nos coment谩rios, come莽ou a aparecer coment谩rios do ultimo post relacionado pelo c贸digo. Voc锚 saberia dizer como solucionar isso?
abra莽o!
@clovis
vou dar uma testada quando puder e tentarei “reproduzir” o erro para achar uma solu莽茫o
@clovis
use o c贸digo php < ?php wp_reset_query(); ?> antes do bloco de c贸digo dos coment谩rios