WooCommerce 从当前类别页面获取父类别 [英] WooCommerce get parent category from current category page
本文介绍了WooCommerce 从当前类别页面获取父类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从 WooCommerce 中的子类别页面获取父类别?经过广泛的研究,这段代码看起来很有希望
How can I get the parent category from a sub category page in WooCommerce? After doing extensive research this piece of code looks promising
get_ancestors( $product_cat_id, 'product_cat' );
但它一直在循环中返回数组".
but it keeps returning 'array' in a loop.
推荐答案
$parentcats = get_ancestors($product_cat_id, 'product_cat');
这是正确的方法,您将获得一个或多个父类别,作为一个数组并循环遍历它以获得如下值:
This is the correct way and you will get one or more parent categories, as an array and loop over it to get the value like below:
foreach($parentcats as $parentcat){
echo $parentcat;
}
这篇关于WooCommerce 从当前类别页面获取父类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文