Categories: Blog

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.

Peter Tran

Share
Published by
Peter Tran

Recent Posts

Kubernetes Resources to Learn From

I have been fortunate in working a lot more with Kubernetes lately over the last few months so I've been…

1 year ago

WordPress Development using Docker

Building my portfolio site, I thought I'd show you how I set up my Wordpress development using Docker. Given the…

1 year ago

Blank images in Puppeteer screenshots solved!

Frustrated with getting blank images in Puppeteer Chrome screenshots, recently I was in a situation where I needed to migrate…

1 year ago

Plans Moving Forward – The Revamped Web and I

It has been six year since my last major revamp/redesign of my current petertran.com.au portfolio site. So what are my…

1 year ago

Bad owner or permissions on .ssh/config

This article helps to solve a Bad owner or permissions on .ssh/config issue occurring on a Windows 10 machine when…

1 year ago

Accept payments easily and effortlessly with Pin Payments integration

Only a few lines of code required is to implement a payment system, thanks to inline Pin Payments integration. Accepting…

6 years ago