0

I'm using get_categories to list all the terms of my taxonomy "genre", but I have other taxonomy called "brands".

In Brands page (taxonomy-brands.php) I need to return all the posts from the brand and the genre they're related.

For example, I have a taxonomy page for Ferrari:

. Brand name: Ferrari

.. Genres: Red, Yellow

But I also have a taxonomy page for Wolkswagen:

. Brand name: Wolkswagen

.. Genre: Blue, Green

The problem is that Ferrari page, is also listing "Blue" and "Green", even if Ferrari doesn't have any posts related to Blue and Green.

Finally, is there any way to hide "Blue" and "Green" when they're not used?

Here is what I got so far: http://codepad.org/rWaVmuUT

If you need a visual example: http://i.imgur.com/PKGpi2o.png - "Sucos" and "CD" should not appear, since this brand only has posts with "Macarrão Instantaneo".

  • 1
    Have you tried Combine two taxonomies in a hierarchical tree? And it is Volkswagen, not Wolkswagen. :) – fuxia Dec 13 '13 at 14:08
  • @toscho They are not parent and child. "genre" is a custom taxonomy and "brand" is another. Sorry for the "W" in Volkswagen. ;) – Pedrão Dec 13 '13 at 14:14
2

Category objects returned by API have count of posts on them in count field. You should simply check that and skip rest of the iteration for those that have no posts.

Something like:

foreach( $categories as $category ) {

    if( 0 == $category->count ) {
        continue;
    }

Scratch that. If I get it right this time what you really need is to check if you got any posts before you output category header. Something like (don't use query_posts() by the way, it's trouble):

$stuff = new WP_Query( $args );

if ( $stuff->have_posts() ) {
    ?><a href="#" class="list-group-item active"><?php echo $category->name; ?></a><?php

    while ( $stuff->have_posts() ) : $stuff->the_post();

        // posts output

    endwhile;
}
  • All the "genre" have posts inside. So it didn't really worked. I'll try to verify if the count is == 0 at this specific "brand"... lets see. Thanks anyway, Rarst! – Pedrão Dec 13 '13 at 18:08
  • @Pedrão yeah, it seems I didn't think it through, it's hard to follow such nested stuff. I guess you would need to check if you queried any posts for the arguments... – Rarst Dec 13 '13 at 18:39

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.