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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With