简体   繁体   中英

WooCommerce get parent category from current category page

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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