Check If Current Page is a Product Category

In this tutorial I will show you two ways how to check if currently displayed page is a product category. We will also try to detect categories with certain IDs, slugs and names.

Are you using is_category() and nothing works?… This tut is just for you! 🎉

Please keep in mind, that product categories in WooCommerce are just a WordPress taxonomy product_cat. It means that it is possible to use all the conditional tags related to taxonomies as well.

But let’s begin with WooCommerce conditional tags first.

Examples:

if( is_product_category() ) {
	// yay, we are on a product category page!
} else {
 
}

It is also possible to pass multiple values in an array:

if( is_product_category( array( 5, 10, 891 ) ) ) {
	// do something for product categories with ID = 5 or 10 or 891
} elseif( is_product_category( 'snowboards' ) ) {
	// do something only for product categories with slug or title "snowboards"
} else {
	// do something else for other pages
}

Do you remember I mentioned before that product categories are taxonomies? So, here is the list of WordPress native conditional tags we can use as well:

This is the example, similar to above:

if( is_tax( 'product_cat', array( 5, 10, 891 ) ) ) {
 
} elseif( is_tax( 'product_cat', 'snowboards' ) ) {
 
} else {
 
}

The reasonable question is – “What is the difference?”

There is no differences, the function is_product_category() is just a wrapper for is_tax( 'product_cat' ).

Well, maybe the only thing you should keep in mind is if you decide to deactivate WooCommerce plugin without changing a theme, all your is_product_category() functions will return error 500 unless you add an additional condition function_exists( 'is_product_category' ).

WooCommerce Conditional Tags

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WooCommerce?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Leave a comment

php js HTML CSS Code