0

The script gets the first tag of a post and retrieves in a wordpress loop all other posts with the same tag. I can't seem to get it working:( Any ideeas?

$posttags = get_the_tags();
$count=0;
if ($posttags) {
  foreach($posttags as $tag) {
    $count++;
    if (1 == $count) {
      $tag = $tag->name . ' ';
    }
  }
}
query_posts('tag='.$tag.''&'showposts=-1'); 
    while (have_posts()) : the_post(); ?>

    <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>

<? endwhile; 
wp_reset_query(); 

Take care

1

the query expects the tag slug, not the tag name:

$tag = $tag->slug . ' ';

and there is an error in the query syntax; corrected:

query_posts('tag='.$tag.'&posts_per_page=-1');

edit: full code rearranged:

$posttags = get_the_tags(); 
if ($posttags) {   
    foreach($posttags as $tag) {     
        $tag = $tag->slug;  break;  
    } 
query_posts('tag='.$tag.'&posts_per_page=-1');      
    while (have_posts()) : the_post(); ?> 

    <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>

    <?php endwhile;  
    wp_reset_query();  
}
|improve this answer|||||
  • Hey tehre, edited the changes but still not working – webmasters Aug 28 '11 at 15:50
  • answer edited; there was an error message with the code before; tested locally. – Michael Aug 28 '11 at 16:29
  • Not sure if you are looking, but how can I make the code, not show the link to the current post, I want to place the code on single.php? – webmasters Aug 28 '11 at 16:36
  • you could get the current post id and use the 'post__not_in' parameter in the query; details in here pastebin.com/ZcxX4rMG – Michael Aug 28 '11 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.