在woocommerce中显示当前产品的最深子类别
这似乎应该很容易做到,但我无法做到,也找不到任何关于它的帖子.
This seems like it should be easy to do but I haven't been able to, and cannot find any posts regarding it.
我可以显示与产品相关的所有类别或顶级类别,但如何仅显示产品的最低/最深类别?
I can display all the categories associated with a product, or the top category, but how do you echo only the lowest / deepest category for a product?
Cat-A [x]
Cat-B [x]
Cat-C [x]
Cat-D [ ]
Cat-E [ ]
Cat-F [ ]
Cat-G [ ]
Cat-H [ ]
如果这个例子是产品的祖先,我只想打印Cat-C".
If this example is the product's ancestry, all I want to print is "Cat-C".
但我不想像其他解决方案一样手动设置类别级别,我希望它始终打印最低的孩子,是存档页面上的产品,或单个产品页面.
But I don't want to manually set the category level like other solutions, I want it to always print the lowest child, be the product on the archive page, or single product page.
知道如何/应该如何做到这一点吗?
Any idea of how this can / should be done?
尝试类似:
// get all product cats for the current post
$categories = get_the_terms( get_the_ID(), 'product_cat' );
// wrapper to hide any errors from top level categories or products without category
if ( $categories && ! is_wp_error( $category ) ) :
// loop through each cat
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;