Woocommerce – determine category of current single product page

In my header.php I want to add title based on the category of the page. My current code looks like this:

             <h1 class="page-title"><?php



                    if (is_category('english') || has_category('english',$post->ID)) {

                        echo "music in the world of noise";

                    } elseif (is_category('marathi') || has_category('marathi',$post->ID)) {

                        echo "क्षितिज जसे दिसते";

                    } elseif (is_category('happenings') || has_category('happenings',$post->ID)) {

                        echo "Happenings";

                    } elseif (is_product() && is_product_category( 'music' ) ) {

                        echo "music";

                    }elseif (is_product_category( 'album' )) {

                        echo "albums";

                    } elseif( is_product() && is_product_category( 'workshop' ) ) {
                       echo "Workshop";
                    } elseif( is_product() && has_term( 'workshop' ) ) {
                       echo "Workshop";
                    } else {the_title();}

                     ?>



                </h1>

I want to echo out Workshop in h1 if the product page is single product page AND if that product is in the workshop category. Same for Music. is_product_category works only on category page not on single product page.

How do I determine the category of single product and echo the relevant text. Other if statements (is_category('english') has_category()) are working except for the woocommerce pages.

Read more here: Woocommerce – determine category of current single product page

Leave a Reply

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