-1

I want the categories and subcategories to be displayed on my single product pages, but I dont want them linking. I've found some really nice code here WooCommerce - how to display product category above product?

That works great but the subcategories are missing... I am not strong in coding so I cant do it myself, someone can modify the code so all categories of a product are showing?

The code I found here was posted by helgatheviking and goes like this:

       function wpa89819_wc_single_product(){

       $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );

        if ( $product_cats && ! is_wp_error ( $product_cats ) ){

       $single_cat = array_shift( $product_cats ); ?>

    <h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>

Thanks in advance! Kelly

1

This is mostly a PHP question and it is related to WooCommerce. Either of which conditions makes it dangerously close to off-topic. However, I am going to give the benefit of the doubt and assume that the problem is due to a misunderstanding of the WordPress function wp_get_post_terms(). ;)

wp_get_post_terms() returns all of the terms as an array of term object. By using array_shift() the way you are you are truncating that list down to one. Remove array_shift() and create a loop.

$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
// var_dump($product_cats); // debug
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
  while (!empty($product_cats)) {
    $single_cat = array_shift( $product_cats ); ?>
    <h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2><?php
  }
}
  • Okay, Thank you very much, I am very new to this to sorry about the misunderstanding... Ok, I'll try and do what you suggest, alldough I dont really understand... I'll try and look further so I can understand the answer you gave. – kelly herman May 14 '15 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.