How to display all the categories and subcategories without link of a product in Woocommerce?

In this tutorial we’ll learn how to display all the categories and subcategories of a product without links in Woocommrce.

Here is our default product screen:

woocommerceNow we need to display all the product categories and sub-categories without links and before product title.

To do this, add the following lines of code at the end of your theme’s functions.php file:

function wpa89819_wc_single_product(){

 

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

 

   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

                                }

       //$single_cat = array_shift( $product_cats ); ?>

 

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

 

<?php }

}

add_action( 'woocommerce_single_product_summary', 'wpa89819_wc_single_product', 2 );

You can access functions.php file here:

woocommercePlace the code and save file by clicking on update file button.

Now, if you’ll go to product page again you’ll see all the categories and sub-categories of product being displayed before product title.

woocommerce

Waqas

I hope you enjoy reading this blog post. If you want my team to do WooCommerce Maintenance for you, click here.

Leave a Comment

Your email address will not be published. Required fields are marked *