This is the code for get subcategory of given category:
$categories = get_the_terms( get_the_ID(), 'product_cat' );
//For checking category exit or not
if ( $categories && ! is_wp_error( $category ) ) :
foreach($categories as $category) :
// get the children (if any) of the current cat
$children = get_categories( array ('taxonomy' => 'product_cat', 'parent' => $category->term_id ));
if ( count($children) == 0 ) {
// if no children, then echo the category name.
echo $category->name;
}
endforeach;
endif;
2 回答
这可以通过自定义函数在其中设置“父”产品类别slug:
代码位于活动子主题(或活动主题)的function.php文件中 .
经过测试和工作 .
Usage example:
您将获得该给定类别的所有子类别的水平昏迷分隔列表(带有链接)
This is the code for get subcategory of given category: