Question

I'd like to display a tag cloud on a site using a custom hierarchical taxonomy.

My problem is that the default wp_tag_cloud displays based on the number of posts for each category/tag. I want it to sum the counts of the children too.

Hope that makes sense, but to clarify

  • Cat1 1 post
    • Subcat1 2 posts
    • Subcat2 1 post
  • Cat2 5 posts

Cat 1 in the tag cloud should have 4 posts attached to it, with greater specificity attained by seeing subcat1 (2 posts) or subcat 2 (1 post). Presently, Cat 1 shows 1 post.

The only thing I can think of at the moment is to capture the results in an array then loop back through them to correct where necessary - Does anyone have a more efficient/elegant way?

Was it helpful?

Solution

So, I looked at the core and was able to resolve this, answer here in case anyone else has the same need.

wp_tag_cloud calls get_terms. get_terms is called with arguments passed from the wp_tag_cloud call, so you can in fact add additional values to alter the default behaviour. Specifically, I can take advantage of 'pad_counts' to solve my problem.

The final call looks like this.

<?php wp_tag_cloud(array('taxonomy'=>'mycustomtaxonomy','pad_counts'=>1)); ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top