1

How do I display the categories of an individual product under the product name on the Shop page listing?

screenshot of Shop page:

enter image description here

aynber
  • 22,380
  • 8
  • 50
  • 63
  • 1
    Questions seeking code help must include the shortest code necessary to reproduce it in the question itself preferably in a [Stack Snippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/). See [How to create a Minimal, Reproducible Example](http://stackoverflow.com/help/reprex). – Tyler2P Aug 19 '21 at 10:08

1 Answers1

0

You should try this to show the product category above the price of each product.

add_filter( 'woocommerce_get_price_html', 'woo_shipping_with_price_display' );
add_filter( 'woocommerce_cart_item_price', 'woo_shipping_with_price_display' );

function woo_shipping_with_price_display( $price ) {
    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
    $text = '';
    if ( $product_cats && ! is_wp_error ( $product_cats ) ){
        $single_cat = array_shift( $product_cats );
        $text = '<div itemprop="name" class="product_category_title"><span>'.__(esc_attr($single_cat->name)).'</span></div>';
        
    }
    
    if ($price  == true) {
        return $text . $price;
    }
    else {
        return $text;
    }
}

You can change/update html,class etc.. from line:

$text = '<div itemprop="name" class="product_category_title"><span>'.__(esc_attr($single_cat->name)).'</span></div>';
Rajeev Singh
  • 1,724
  • 1
  • 6
  • 23