简体   繁体   中英

WooCommerce - determine product 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?

There is some mistakes in your code regarding WooCommerce categories:

  • Functions is_category() and has_category() doesn't work with WooCommerce product categories (WooCommerce product categories are a custom taxonomy ' product_cat ') .
  • Conditional is_product() && is_product_category() will not work together with && , as is_product() will target single product pages and is_product_category() will target product categories archives pages.

To target your product categories in single product pages, you need to use Wordpress conditional function has_term() with ' product_cat ' taxonomy.
You can also target product categories archives pages at the same time in your condition, if they use the same title…

So your code (for WooCommerce product categories) is going to be something like:

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

//      ... / ...

    if ( is_product() && has_term( 'music', 'product_cat' ) || is_product_category( 'music' ) ) {
        echo "Music";
    } elseif ( is_product() && has_term( 'album', 'product_cat' ) || is_product_category( 'album' ) ) {
        echo "Albums";
    } elseif( is_product() && has_term( 'workshop', 'product_cat' ) || is_product_category( 'workshop' ) ) {
        echo "Workshop";
    } else {
        the_title();
    }
?></h1>

If you don't need to target at the same time product categories archives pages, you will have to remove in each condition statement || is_product_category() || is_product_category()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM