简体   繁体   English

WooCommerce 从当前类别页面获取父类别

[英]WooCommerce get parent category from current category page

How can I get the parent category from a sub category page in WooCommerce?如何从 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM