How To Check Current Page Is A Product Category

Here we will learn about how to check current page is a product category.

There is two way to check page is a product category.

We will also find categories with certain IDs, slugs, and names.

1. is_product_category():

The WooCommerce product taxonomy is ‘product_cat’.

Let’s see WooCommerce conditional tags.

  • is_product_category() – returns true, every product category archive page.
  • is_product_category( $category ) – if you check product category.

Examples:

<?php
if( is_product_category() ) {
  // do something here...
} else {
  // do something here...
}
?>

It is also possible to pass multiple categories and slugs.

<?php
if( is_product_category( array( 3, 5, 9 ) ) ) {
  //  product categories with ID = 3 or 5 or 9
  // do something here...
} elseif( is_product_category( 'bmw' ) ) {
  // product categories with slug or title "bmw"
  // do something here...
} else {
  // do something here...
}
?>

2. is_tax( ‘product_cat’ ):

This function is similler like is_product_category().

Examples like same as above:

<?php
if( is_tax( 'product_cat', array( 3, 5, 9 ) ) ) {
  // do something here... 
} elseif( is_tax( 'product_cat', 'bmw' ) ) {
  // do something here...
} else {
  // do something here...
}
?>

 

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories