Al final de nuestros posts, siempre es agradable para el lector tener una lista de artículos relacionados, por si deseara continuar su búsqueda en nuestro blog. Aprovechando el sistema de etiquetas que WordPress nos brinda, podremos, con el siguiente código, disponer de tal lista.

A notar que estaremos tabajando aquí con etiquetas y no categorías, que requerirían otro código (y que en nuestro siguiente artículo desvelaremos), y que por tanto cuantos más artículos compartan etiqueta, más útil sera el plan:

<?php
//for use in the loop, list 5 post titles related to first tag on current post
$backup = $post; // backup the current object
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
}
$post = $backup; // copy it back
wp_reset_query(); // to use the original query again
?>

Vamos simplemente a pastear éste código dentro del Loop de nuestro single.php, o index.php, según queramos mostrarlo en la página principal de nuetsro site debajo de cada último artículo, o/y en la página singular correspondiente.

Gracias a Mike por este gran código.