Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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();  
}
share|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
    
Ty, working like a charm;) Really thank u –  webmasters Aug 28 '11 at 16:30
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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