如何列出 woocommerce 上的子类别?

How can I list the subcategories on woocommerce?

这个问题很"simple",不过,我还没有找到解决这个问题的好方法。

我想做的是,在应用过滤器之前,我想获取子类别列表(我需要它们的 ID)。

所以,我试过这个

$args = array( 'hierarchical' => 1, 
    'show_option_none' => '', 
    'hide_empty' => 0, 
    'parent' => 12, 
    'taxonomy' => 'product_cat' ); 
$subcats = get_categories($args);

但好像有些地方不太好,我明白了:

Array ( [WP_Errorerrors] => Array ( [invalid_taxonomy] => Array ( [0] => Invalid taxonomy ) ) [WP_Errorerror_data] => Array ( ) ) 

如果我将代码移到我的过滤器所在的位置,我会遇到无限循环错误(更糟糕的是),但是,我还没有找到为什么它是无效的分类法...有没有更简单的方法来解决这个问题?

谢谢。

get_categories() automatically assumes the taxonomy is category. Product Category is a custom taxonomy (product_cat), so you need to use get_terms()

function sub_cats(){
    $args = array( 'hierarchical' => 1, 
        'hide_empty' => 0, 
        'parent' => 12 ); 
    $subcats = get_terms( 'product_cat', $args);

var_dump($subcats); 
}
add_action('init', 'sub_cats', 20 );