Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 923
mysticalghoul Avatar asked Dec 20 '16 14:12

mysticalghoul


1 Answers

$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;
}
like image 89
shakeel osmani Avatar answered Oct 17 '22 17:10

shakeel osmani