Support » How-To and Troubleshooting » [Resolved] Is there a way to count the number of subcategories a category has?

[Resolved] Is there a way to count the number of subcategories a category has?

  • Michelle Langston


    Theme Wrangler at Automattic

    Hello, I’m making a newspaper-style theme. On my site, I have several categories that have sub-categories, and some that don’t. For example, “Graphic Design” contains the sub-categories “Web Design”, “Print Design” and “Motion Design.” But “News” contains zero sub-categories.

    On the “Graphic Design” category page, I want “Web Design,” “Print Design” and “Motion Design” to show up in the menu. I want those 3 subcategories to show up even when you click on “Web Design”, “Print Design” or “Motion Design.”

    I DON’T want any submenus showing up when I click on “News”, because “News” has zero subcategories.

    Here is the code I’m using:

    if (is_category()) {
    $this_category = get_category($cat);
    $this_category = wp_list_categories('orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent."&echo=0"); else
    $this_category = wp_list_categories('orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
    if ($this_category) { ?>
    <?php echo $this_category; ?>
    <?php } ?>

    So far, it does everything I want, EXCEPT on “News” (which has zero subcategories), it prints out “No Categories” along with an empty
    list. How do I get this code to ONLY execute if a category has at least ONE subcategory? I don’t want the sub-category mark-up to show up if there are no categories, and I don’t want the “No Categories” message to show.

    The following code is what I was using previously, which showed subcategory markup ONLY when a category contained at least one subcategory:

    <?php if (is_category()) {
      $this_category = get_category($cat);
      if (get_category_children($this_category->cat_ID) != "") { ?>
       <!--START SUB CATEGORY --><div class="row" id="subCategory">
        	<!--Sub Category-->
            <div class="column grid_12">
    		<!--subcats--><div id="subcats">
    		<li class="sub_title">Subcategories:</li>
       <?php wp_list_categories('depth=1&orderby=id&show_count=0&title_li=
    		</div> <!--/column-->
    <?php  }
    	} ?>

    But this code hides the rest of the sibling categories once you click on one of them (e.g. if you clicked on “Print Design”, you no longer see “Web Design or “Motion Design.”).

    What I’m looking for is a way to first count how many subcategories a category contains, and if that number is greater than zero, apply the first block of code. It would involve this line from the second block: if (get_category_children($this_category->cat_ID) != "") { but if I add it to the first block it doesn’t work.

    Any suggestions would be greatly appreciated. I’m not an expert on PHP so this is a learning experience for me.

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Is there a way to count the number of subcategories a category has?’ is closed to new replies.