0

Basically I have regular Category Listings that include posts WITH tags and posts WITHOUT tags.

I need to modify the loop so that it ONLY includes posts which have one or more tags assigned to them.

Is there a way do this?

Thanks

1 Answer 1

3

here is an example loop..
you can change the number of tag to exclude based on the amount you want...

<?php 
if (have_posts()) : while (have_posts()) : the_post();

// GET THE TAGS OF CURRENT POST IN LOOP AND COUNT THE AMOUNT
    $posttags = get_the_tags();
    $howmany = count($posttags);
    if($howmany > 2) {
?>
    <h3><?php the_title(); ?></h3>
    <?php the_excerpt(); ?>

<?php
    } else {
    // do nothing
    }
    endwhile; 
?>
<?php endif; ?>

.

Hope This Helps
Sagive

7
  • The Tag ID will not be known. I only want the loop to display posts which HAVE a tag set. There's over 100 tags. The posts WITH tags usually have 3 - 5. I only want them to appear in category listings.
    – clarkeyo
    Jun 16, 2012 at 6:45
  • i see.. sorry - missread you question.. fixed the solution.
    – Sagive
    Jun 16, 2012 at 7:04
  • Thanks. That does exactly what I want. The only problem is that it now doesn't show the right posts per page. For example: I have 10 posts per page, and there's 120 posts (with tags). Instead of showing just 12 paged pages with 10 posts each, it shows about 25 paged pages with varying amounts of posts per page - between 2 and 8.
    – clarkeyo
    Jun 16, 2012 at 7:16
  • paste your current loop so i wont mass it up and i would gladly help
    – Sagive
    Jun 16, 2012 at 8:59
  • Currrent loop looks like this: <?php $posts = query_posts( $query_string . '&orderby=title&order=asc&post_type=glossary' ); ?> <?php if (have_posts()) : while (have_posts()) : the_post(); // GET THE TAGS OF CURRENT POST IN LOOP AND COUNT THE AMOUNT $posttags = get_the_tags(); $howmany = count($posttags); if($howmany > 2) { ?> <div class="cat-posts"> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <?php the_excerpt(); ?> </div> <?php } else { // } endwhile; ?> <?php endif; ?>
    – clarkeyo
    Jun 16, 2012 at 14:38

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.