Mostrar post relacionados en wordpress sin plugins

7 feb 2010 1 comentario

Si no queremos usar un plugin para mostrar los post relacionados de un determinado post en wordpress podemos utilizar el siguiente script php. Este código se mostrará los post relacionados con las etiquetas o tags. Este script debe ser utilizado dentro del loop de wordpress.

$tags = wp_get_post_tags($post->ID);
if ($tags) {
  $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() ) {
    echo '<ul>';
    while ($my_query->have_posts()) : $my_query->the_post();
      echo '<li><a href="'.the_permalink().'" rel="bookmark">'.the_title().'</a></li>';
    endwhile;
    echo '</ul>';
  }
}

1 Comentarios

Juan, el 09/07/2011 a las 06:33:59

Ahora si que bueno bonito y barato..Grax testing…

Escribe un comentario

Comenta este post en Facebook