Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have categories:

-Parent category
--Subcategory
---Grandcategory

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 );
share|improve this question
1  
As an alternative you could switch from get_categories() to wp_list_categories() and use the 'depth' parameter... –  Sven Jul 13 '13 at 6:55
    
Thanks, I'm try. –  thai Kont Jul 14 '13 at 8:15

1 Answer 1

up vote 1 down vote accepted

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]);
        }
    }
}
share|improve this answer
    
remove my answer since it went nowhere. Keep yours in my favorite. –  JMau Jul 13 '13 at 23:59
    
yeahh. Thank you. I'm sorry but i cannot vote for you. Thank again. –  thai Kont Jul 14 '13 at 8:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.