Check if Product Belongs to a Product Category or Tag
Are you trying to make it work with in_category()
function but nothing happens? Well, in this tutorial you will find out why.
Actually it is a very common mistake and I also did it before.
First things first, let’s begin with the fact that products in WooCommerce are a custom post type product
and product categories are a custom taxonomy product_cat
. When you work with custom taxonomies, do you use functions for categories/tags or for taxonomies?
Comparing to is_product_category(), WooCommerce doesn’t have its own conditional tags here, so we have to use default WordPress conditional tags like has_term()
.
Check If Current Page is a Product Category
Are you using is_category()
and nothing works?… This tutorial is just for you then.
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.
is_product_category()
– returnstrue
on every product category archive page,is_product_category( $category )
– you can check if you are on a certain product category page just by passing an ID, slug or title of a category as an argument.