Detecting Category and Tag Pages in Woocommerce

Learning WooCommerce Development by Example
A Friendly and Accessible Guide to WooCommerce Development
Click here to get the book now for $29

The Problem

When adding functionality to a WooCommerce store we sometimes want to add features that should only be present on category or tag pages. In this article, we’ll take a look at how we can detect category and tag pages in our WooCommerce code.

The Solution

This code will detect if the user is on a category page

//returns true if the current page is a category page
is_product_category();

//returns true if the current page is the category page specified in the passed argument
is_product_category('shirts');

//returns true if the current page is any of the category pages specified
//in the passed array
is_product_category(array('shirts', 'games' ));

//also works with numerical ids
is_product_category(18);
is_product_category(18,19,20);

The code for detecting a tag page is very similar

//returns true if the current page is a category page
is_product_tag();

//returns true if the current page is the tag page specified in the passed argument
is_product_tag('puzzles');

//returns true if the current page is any of the tag pages specified
//in the passed array
is_product_tag(array('jigsaws', 'toys' ));

//also works with numerical ids
is_product_tag(18);
is_product_tag(18,19,20);

Discussion

There’s not really an awful lot more to say about this as the code itself is pretty self-explanatory. If you do have any questions then please don’t hesitate to let us know in the comments.

Hey there! Hope you enjoyed that article, thanks for sticking with it until the end 🙂

If you enjoy our WooCooomerce writing you might enjoy our book "Learning WooCommerce Development By Example", it's only $29 and contains a multitude of useful information for WooCommerce developers. Click here for more details.

You might also like to sign up for our newsletter, where you'll be kept to date with all our latest WooCommerce articles. Just complete the form below to get the latest Hard Working Nerd WooCommerce content delivered straight to your inbox.

Leave a Comment