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

Time:02-13

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?

CodePudding user response:

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.

  • Related