WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Is there a way to count the number of subcategories a category has? (4 posts)

  1. Michelle Langston
    Theme Wrangler at Automattic
    Posted 4 years ago #

    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:

    <?php
    if (is_category()) {
    $this_category = get_category($cat);
    }
    ?>
    <?php
    if($this_category->category_parent)
    $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) { ?>
    
    <ul>
    <?php echo $this_category; ?>
    </ul>
    
    <?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">
    		<ul>
    		<li class="sub_title">Subcategories:</li>
       <?php wp_list_categories('depth=1&orderby=id&show_count=0&title_li=
    &use_desc_for_title=1&child_of='.$this_category->cat_ID);?>
        </ul>
    		</div><!--/subcats-->
    		</div> <!--/column-->
          </div><!--/subCategory-->
    <?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.

  2. Michelle Langston
    Theme Wrangler at Automattic
    Posted 4 years ago #

    Any suggestions? I've tried multiple things but am still having no luck. :-(

  3. Michelle Langston
    Theme Wrangler at Automattic
    Posted 4 years ago #

    This thread seems to have a solution to the problem I was having: http://wordpress.org/support/topic/307248?replies=28

    I will try it out and report back. If I can get it working then it's all good. Hopefully it will help someone else who is having the same issue.

  4. Michelle Langston
    Theme Wrangler at Automattic
    Posted 4 years ago #

    Okay, the problem is solved, thanks to this thread: http://wordpress.org/support/topic/307248?replies=28

    The last bit of code posted by the user t31os_ works like a charm for me! Thanks t31os_ :-)

    Hopefully this helps someone else in the future who runs into this issue!

Topic Closed

This topic has been closed to new replies.

About this Topic