Displaying Only One Level Of Child Categories in Wordpress

Hello,
I have a theme that was purchased called “The Clothes Shop”. However the developer no longer supports or does custom work of this theme. So support is limited to just some FAQs on the site.

I ran into an issue where the sub navigation section will display all child, grandchild and subs no matter what parent category you are in. I would like to only show one level deep on whatever category I’m currently on. I’ve tried fooling around with the depth parameters in the code, but I’m not as familiar with PHP to fully comprehend the change I’m making. Seems like it should work by changing those values.

Bellow is the full include file of the code to display the sub navigation section.


<?php if (is_single() && $OPTION['wps_linksTop_enable']) {$the_div_class="SubCatNav single_SubCatNav";} else {$the_div_class="SubCatNav";}?>
<div class="<?php echo $the_div_class; ?>">
    <ul class="viewAll">
        <?php
        if (is_category()) {
            if ($this_category->category_parent == $topParent->term_id){ 
                if ($catDepth==1) { ?>
                    <li><a href="<?php echo get_category_link($this_category->parent);?>"><?php _e('View All','wpShop');?></a></li>
                <?php } else { ?>
                    <li class="active"><a href="<?php echo get_category_link($this_category->term_id);?>"><?php _e('View All','wpShop');?></a></li>
                <?php }
            } else { ?>
                <li><a href="<?php echo get_category_link($this_category->parent);?>"><?php _e('View All','wpShop');?></a></li>
            <?php }
            
        } elseif (is_single()) {
            if ($category[0]->category_parent != 0){ 
                if ($catDepth==1) { ?>
                    <li><a href="<?php echo get_category_link($category[0]->term_id);?>"><?php _e('View All','wpShop');?></a></li>
                <?php } else { ?>
                    <li><a href="<?php echo get_category_link($category[0]->category_parent);?>"><?php _e('View All','wpShop');?></a></li>
                <?php }
            } else { ?>
                <li><a href="<?php echo get_category_link($category[0]->term_id);?>"><?php _e('View All','wpShop');?></a></li>
            <?php }
        
        } else {} ?>
    </ul>

    <?php
    $orderby     = $OPTION['wps_catSubNav_orderbyOption'];
    $order         = $OPTION['wps_catSubNav_orderOption'];
    
    if (is_category()) {
        if($this_category->category_parent == $topParent->term_id){
        
            $cats = explode("<br />",substr(trim(wp_list_categories('title_li=&echo=0&orderby='.$orderby.'&order='.$order.'&child_of='.$this_category->term_id.'&hide_empty=0&style=none')),0,-6));
            
            if (trim($cats[0]) == 'No cate') {
                $cats = explode("<br />",substr(trim(wp_list_categories('title_li=&echo=0&orderby='.$orderby.'&order='.$order.'&child_of='.$this_category->parent.'&hide_empty=0&depth=1&style=none')),0,-6));
            }
        } else {
            $cats = explode("<br />",substr(trim(wp_list_categories('title_li=&echo=0&orderby='.$orderby.'&order='.$order.'&child_of='.$this_category->parent.'&hide_empty=0&style=none')),0,-6));
        }    
    } elseif (is_single()) {
    
        if ($category[0]->category_parent != 0){
            if ($catDepth==1) {
                $cats = explode("<br />",substr(trim(wp_list_categories('title_li=&echo=0&orderby='.$orderby.'&order='.$order.'&child_of='.$category[0]->term_id.'&hide_empty=0&style=none')),0,-6));
                if (trim($cats[0]) == 'No cate') {
                    $cats = explode("<br />",substr(trim(wp_list_categories('title_li=&echo=0&orderby='.$orderby.'&order='.$order.'&child_of='.$category[0]->category_parent.'&hide_empty=0&depth=1&style=none')),0,-6));
                }
            } else { 
                $cats = explode("<br />",substr(trim(wp_list_categories('title_li=&echo=0&orderby='.$orderby.'&order='.$order.'&child_of='.$category[0]->category_parent.'&hide_empty=0&style=none')),0,-6));
            }
        } else {
            $cats = explode("<br />",substr(trim(wp_list_categories('title_li=&echo=0&orderby='.$orderby.'&order='.$order.'&child_of='.$category[0]->term_id.'&hide_empty=0&style=none')),0,-6));
        }
    } else {}
    
    //only run the foreach if categories were found
    if (trim($cats[0]) != 'No cate') {  
        //how many rows?
        $wanted_rows    = $OPTION['wps_catSubNav_rows'];
        $num            = count($cats);
        $counter        = 1;            
        $container         = '<ul>';
        
        foreach($cats as $cat){
            //set an active class on the current category                
            $parts1 = explode("</a>",$cat);                        
            $parts2 = explode(">",$parts1[0]);
            $catnam    = trim($parts2[1]);    
            if (is_category()) {
                $the_li_class = ($this_category->name == $catnam ? 'active' : NULL);
            } elseif (is_single()) {
                $the_li_class = ($category[0]->name == $catnam ? 'active' : NULL);
            } else {}
            
                if($counter % $wanted_rows == 0){
                    $container .= "<li class='$the_li_class'>".$cat .'</li></ul><ul>';                    
                } else {
                    $container .= "<li class='$the_li_class'>".$cat.'</li>';
                }
            
            $counter++;
        }
        
        $ending = substr($container,-4);
        if($ending == '<ul>'){
            $container = substr($container,0,-4);
        }
        
        echo $container;
    } ?>
</div>

Would this be better suited for the PHP thread?

No, this is the place.

I know it can be a bit daunting until you get used to it, but the Codex is your friend.

From http://codex.wordpress.org/Template_Tags/wp_list_categories

depth
(integer) This parameter controls how many levels in the hierarchy of Categories are to be included in the list of Categories. The default value is 0 (display all Categories and their children). NOTE - From #L289: The ‘hierarchical’ argument, which is disabled by default, will override the depth argument, unless it is true. When the argument is false, it will display all of the categories. When it is enabled it will use the value in the ‘depth’ argument. This parameter added at Version 2.5

Try adding hierarchical=1

Thanks for that. I tried adding the hierarchical=1 setting and it did not change the way it was displayed. I did however add in &depth=1 in all the wp_list_categories. This got rid of the extra sub grandchild categories.

However, because of this setting, when I’m on the grandchild categories, their sub categories no longer show up. So I will have to play with that a bit or figure out why the hierarchical setting did nothing. It could be because of the $catDepth==1 parameter overriding the inline depth setting?