简体   繁体   English

WooCommerce - 确定当前单个产品页面的产品类别

[英]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. 在我的header.php我想根据页面的类别添加标题。

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. 如果产品页面是单个产品页面并且该产品属于workshop类别,我想在h1中回显Workshop Same for Music . Music is_product_category works only on category page not on single product page. is_product_category仅适用于不在单个产品页面上的类别页面。

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? 除了woocommerce页面之外,其他if语句( is_category('english') has_category() )是否正常工作?

There is some mistakes in your code regarding WooCommerce categories: 您的代码中有一些关于WooCommerce类别的错误:

  • Functions is_category() and has_category() doesn't work with WooCommerce product categories (WooCommerce product categories are a custom taxonomy ' product_cat ') . 函数is_category()has_category()不适用于WooCommerce产品类别(WooCommerce产品类别是自定义分类' 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. 条件is_product() && is_product_category()不能与&&一起使用,因为is_product()将定位单个产品页面, is_product_category()将定位产品类别归档页面。

To target your product categories in single product pages, you need to use Wordpress conditional function has_term() with ' product_cat ' taxonomy. 要在单个产品页面中定位您的产品类别,您需要使用WordPress条件函数has_term()和“ product_cat ”分类法。
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: 所以你的代码(对于WooCommerce产品类别)将是这样的:

<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() || is_product_category() || is_product_category() ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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