Home Woocommerce - add last category name to product name
Reply: 1

Woocommerce - add last category name to product name

Bartosz XxX
1#
Bartosz XxX Published in 2018-02-14 05:23:34Z

How to display last category name for product ? I want to combine the product name with the name of the last category https://screenshotscdn.firefoxusercontent.com/images/0b7c1ee5-6b2a-4e26-bf9e-1c44084abc5c.png

Ninjo
2#
Ninjo Reply to 2018-02-14 05:33:34Z

Try something like this:

// get all product cats for the current post
$categories = get_the_terms( get_the_ID(), 'product_cat' ); 

// wrapper to hide any errors from top level categories or products without category
if ( $categories && ! is_wp_error( $category ) ) : 

// loop through each cat
foreach($categories as $category) :
  // get the children (if any) of the current cat
  $children = get_categories( 
      array ('taxonomy' => 'product_cat', 'parent' => $category->term_id )
  );

  if ( count($children) == 0 ) {
      // if no children, then echo the category name.
      echo $category->name;
  }
endforeach;

endif;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306061 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO