mysticalghoul
mysticalghoul

Reputation: 672

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.

Upvotes: 9

Views: 27149

Answers (1)

shakeel osmani
shakeel osmani

Reputation: 736

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

Upvotes: 19

Related Questions