WordPress WPCommerce – Get the parent category of a category object

Maintaining a WordPress site that has WP Commerce set up installed, I needed to do some minor customisations to a widget that displayed nested Category names. Because some categories shared identical names as other categories of the same level (e.g. ‘Necklaces > Classic’ and ‘Earrings > Classic’), I needed to prepend the parent category name to a menu link to the category that I was creating.

As this information was not readily available on the Internet, I had fiddled around with the WP commerce source.

If you have the current category in a data object, this is perfect. So to retrieve the parent category from a category data object in WPCommerce, simply do:

$parent = get_term((int) $cat->parent, 'product_cat' );
echo $parent->name;

You should then be able to access the parent category in a data object so then you’re free to access it’s properties accordingly.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.