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'm working on a pictures gallery, where there is no 'regular' post, only images with several custom taxonomies, some of them being hierarchical.

I use wp_list_categories() to display hierarchical lists of terms and that works fine provided I set the hide_empty parameter to 0. However, I would like to display only terms that have pictures, or whose descendants have pictures. But I can't find a way to use hierarchical instead of hide_empty, probably because the function looks for terms attached to regular posts.

Is there a way to change its behaviour and make it look for post-type='attachment' or whatever, so I could list only terms related to published pictures?

Thanks for helping.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

When you register your taxonomy, set the update_count_callback argument to _update_generic_term_count. This will count all objects associated to taxonomy terms so you don't need to use hide_empty.

$args = array(
    // your other taxonomy arguments here
    'update_count_callback' => '_update_generic_term_count'
);
register_taxonomy( 'your-taxonomy', array( 'attachment' ), $args );
share|improve this answer
    
Is this supposed to work with the wp_list_categories() function too? Because I can't see any difference with the _update_generic_term_count parameter. It's either hide_empty set to false and I get everything; or hide_empty is true and the only children categories I get are those containing media attached to some post (the home page in fact, as it is the only one that uses attached media). And by the way, the pad_counts parameter doesn't seem to work either. –  tosca May 19 at 6:50
    
If you just changed that parameter then term counts are still wrong since you've associated the attachments before making that change. You either need to manually update the term count field or re-add terms to attachments to fix the count. –  Milo May 19 at 13:38
    
Thanks. That seems to have done it, though I'm a bit surprised I had only to remove/add each term from one media only to get the counter right. –  tosca May 20 at 8:44

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.