Parent Category and Subcategory default text

sicario

Member
Oct 21, 2018
36
11
8
Hello all,
Would be grateful if someone can help me with the below.

I have several category and sub category on my page. For E.g

Food
-Apple
-Orange

Shirt

Laptop
-HP
-Dell

I would like on each category page and sub category page a default text appearing as so:

For sub category

The best Food Apple you can get.
Taking the main category follow by the sub category

If there is no subcategory only outputting the main category

The best Shirt you can get

This should be the same for all category and subcategory page.

I am using the below code but is able to output only the main category

Code:
 <h2 class="supply">The best 

<?php   
$cat = get_the_terms( $product->ID, 'product_cat' );

foreach ($cat as $categoria) {
if($categoria->parent == 0){
   echo $categoria->name;
?>  
you can get </h2>
 

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu