如何在 WooCommerce 的类别页面上为每个产品标签回显不同的内容?

How do I echo different content for each product tag on a category page in WooCommerce?

我正在使用 URL 参数来过滤我的类别,如下所示:https://example.com/collections/trousers/?product_tag=male

我想根据使用的标签回显不同的 HTML。这是我的代码,应该很容易解释:

add_action('bs_after_primary', 'custom_product_category_descriptions');

function custom_product_category_descriptions() {

if ( is_product_category( 'trousers' ) && ! is_product_tag( array( 'male', 'female' ) ) ) {
echo '<h1>Checkout these trousers for men and women</h1>';
  } elseif ( is_product_category( 'trousers' ) && is_product_tag ( 'male' ) && ! is_product_tag ( 'female' ) ) {
echo '<h1>Checkout out these trousers for Men</h1>';
  }
    elseif ( is_product_category( 'trousers' ) && ! is_product_tag ( 'male' ) && is_product_tag ( 'female' ) ) {
echo '<h1>Checkout out these trousers for Women</h1>';
  }
}

以上代码在所有裤子类别页面上的<h1>Checkout these trousers for men and women</h1>

https://example.com/collections/trousers/

https://example.com/collections/trousers/?product_tag=male

https://example.com/collections/trousers/?product_tag=female

为什么代码会在所有 URL 上面回显而忽略我的 !不是条件语句吗?

Woo 的 is_product_tag() 功能检测您是否正在查看产品标签页。但是您正在查看产品类别页面,而不是标签页面,所以 is_product_tag( anything ) 总是 returns false.