How to display only subcategory

I have categories:

-Parent category
–Subcategory
—Grandcategory

Read More

How i can display parent category and only subccategory(not display grandcategory).
My args:

$args = array(
                            'type'                     => 'product',
                            'child_of'                 => $category_ID,
                            'parent'                   => '',
                            'orderby'                  => 'name',
                            'order'                    => 'ASC',
                            'hide_empty'               => 0,
                            'hierarchical'             => 1,
                            'exclude'                  => '',
                            'include'                  => '',
                            'number'                   => '',
                            'taxonomy'                 => 'product_cat',
                            'pad_counts'               => true );

and i query :

$categories = get_categories( $args );

Related posts

1 comment

  1. The easiest way to do this would be to use wp_list_categories instead. It has depth parameter which will do exactly what you want (you can use custom walker class, if you need different output than default one).

    Another way would be to write your own code (but it won’t be a beautiful one). You can use something like this:

    $args = ...
    $categories = get_categories( $args );  // $category_ID as child_of
    foreach ($categories as $k=>$category) {
        if ( $category->parent != $category_ID) {  // it's not direct child
            $parent_category = get_term($category->parent, 'product_cat');
            if ( $parent_category->parent != $category_ID ) {  // it's not grandchild either
                unset($categories[$k]);
            }
        }
    }
    

Comments are closed.