Archived topic
parent category before product title on single product page
5 replies · Started by Hennie on January 25, 2022
Hi Guys, I hope you are able to help me.

At the moment the situation is like the image above.
In the breadcrumb you see Home/parent category (yellow)/sub category (dark-blue)/product title (light blue).
Right from the image you see from top to bottom: sub category (dark blue) / product title (light blue) / price etc.
I like this sub category (dark blue) to show the parent category (yellow)
Sometimes a product has more steps between parent-category and product.
This doesn't matter, I always want this to be the parent category.
Hopefully you are able to help.
Hi Hennie,
To show the sub category is defined by Woocommerce, the theme has no control over it Unfortuanly.
I would recommend reach out to Woocommerce, they might be able to help :)
Thank you Ying,
Will do! If I find out I'll post the answer here.
Thanks Hennie :)
Hi Ying,
Here I am again,
Searching the internet I found many non-working code but this snippet works for me:
<?php
$terms = get_the_terms( $product->ID, 'product_cat' );
foreach ($terms as $term) {
if($term->parent == 0){
echo $term->name;
}
};?>
It looks almost too simple to be true, but I'm happy it works.
Credits go to Mauro
Versions used:
Generatepress 3.1.1
GP premium 2.1.1
Wordpress 5.9
Woocommerce 6.1.1
Thank you very much for sharing this :)
I'm sure it will be useful for other users!