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.

I am trying to write a query post such that it returns a list fixed list of posts from a category along with any of the one tag associated with it. For eg:

<?php $arg = array('cat' => '1','showposts' => 10,'offset' => 0); query_posts($arg); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<a class="post-link" title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php $posttags = get_the_tags(); $count=0; if ($posttags) { foreach($posttags as $tag) { $tag_link = get_tag_link($tag->term_id); $count++; if ($count == 1) 
{ echo '<a class="tag-link" href="'.get_tag_link($tag->term_id).'">#'.$tag->name.'</a>'; } } } ?> 

Posts in Topic A

Post_Title_1 #tag1
Post_Title_2 #tag2
Post_Title_3 #tag3
Post_Title_4 #tag2 <-- I don't wan't this Post one as Tag2 has been displayed already.

The issue is I want only posts with unique tags to be shown.... How can i do that... Please help

share|improve this question

1 Answer 1

up vote 0 down vote accepted
<?php
$arg = array('cat' => '1','showposts' => 10,'offset' => 0);
query_posts($arg);
if (have_posts()) : while (have_posts()) : the_post();
?>
<a class="post-link" title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
if (!$used_tags) $used_tags = array();
$posttags = get_the_tags();
if ($posttags) {
    foreach ($posttags as $tag) {
        if ( !in_array($tag->term_id, $used_tags) ) {
            array_push($used_tags, $tag->term_id);
            echo '<a class="tag-link" href="'.get_tag_link($tag->term_id).'">#'.$tag->name.'</a>';
            break 2;
        }
    }
}
?>

Not tested though!

share|improve this answer
    
Changed break 2; to break; and its working fine. Thanks. I also needed to filter out posts in hidden tag so just added && $tag->slug != 'hidden' after !in_array($tag->term_id, $used_tags) and its working fine too (if anyone is using this code). –  Debajyoti Das Jun 20 '12 at 13:56

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.