Home > Net >  How to displaying product category before product title?
How to displaying product category before product title?

Time:11-26

I have been trying to display the product sub-category name above the product title on the Shop or Product Archive page.

I've tried the code below but I am not sure how to get sub-categories instead of the category:

function category_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>

CodePudding user response:

you can use get_term_children() function

$list_sub_cats = get_term_children($single_cat->term_id,'ca');

I hope this will help you, Visit to get more info enter image description here

  • Related