简体   繁体   中英

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

I am using URL parameters to filter my categories like so: https://example.com/collections/trousers/?product_tag=male

I want to echo different HTML depending on the tag used. Here's my code which should be pretty self explanatory:

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>';
  }
}

The above code echo's <h1>Checkout these trousers for men and women</h1> on all trouser category pages:

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

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

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

Why does the code echo on all URL's above ignoring my ! is not conditional statements?

Woo's is_product_tag() function detects whether you are viewing a product tag page. But you are viewing a product category page, not a tag page, so is_product_tag( anything ) always returns false .

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