我有以下类别:
-父类别--子类别--大类别
如何显示父类别而仅显示子类别(不显示grandcategory)。我的参数:
$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 );
 我问:
$categories = get_categories( $args );
 
                    最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成
                    最简单的方法是使用wp_list_categories 相反它有depth 参数,它将完全执行您想要的操作(如果您需要不同于默认输出的输出,则可以使用自定义walker类)。
另一种方法是编写自己的代码(但它不会很漂亮)。您可以使用以下内容:
$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]);
        }
    }
}