How to Check if the Current Page is a Category in WordPress!

2.3333333333333 from 5
from 3 user
(Rate this post)
How to Check if the Current Page is a Category in WordPress!

Categories are actually WordPress terms under the hood, like tags and other classification entities. But there is a simple solution in WordPress to detect a category page. WordPress developers can use the is_category function to check if the current page is a category or not. This WP function accepts one argument. Using that argument, you can determine if the current page is a specific category page or one of the specified categories. We're going to explain how to use the is_category function with a perfect example. Read this WordPress tutorial to learn how to check if a category is currently being displayed in a visitor's browser.

Check if the current page is a category using is_category function

The is_category function can check if the current page is a category page or not.

The syntax has everything you need to know:

is_category(int|string|int[]|string[] $category = '')

This function only accepts a single argument to specify the category (or categories) you want to check if the current page is presenting.

The $category can be a single value of type integer or string. If a single value is declared (not an array), WP will check if the current page is a category matching the given slug, name, or ID.

If you have multiple values, to check if the current page is a category matching any of the given array values, you can declare an array of IDs (integer), category names (string), or slugs (string). It is possible to use them mixed together.

Keep reading to find examples for each use case…

Check if the current page is a category page (not specified)

//just use the function with no argument
if(is_category()){
	//this page is a category page

}
else{
	//this page is NOT a category page

}

Check if the current page is a specified category

Providing a single value of type Integer to String for the is_category function will result in checking the current page for a specified category.

Check by category ID:

Using integer value will result in checking the current page for a specific category by ID.

$categoryId = 7; //Category with term_id = 7
if(is_category($categoryId)){
	//this page is the exact category

}
else{
	//this page is NOT the specified category page
	//but it can be another category, or not a category page

}

Check by category slug:

Developers can use String value to check the current page for a specific category by slug (or name in the next example).

In this example, we checked if the current query belongs to a category with a specific slug (uncategorized):

$categorySlug = "uncategorized";
if(is_category($categorySlug)){
	//this page is the exact category page

}
else{
	//this page is NOT the specified category page
	//but it can be another category, or not a category page

}

Check by category name:

Category names can have spaces, and their letters are not forced lower case, which makes them different from slugs.

If you declare the category you want to check with a String, WP also will check the page for a category with the given name (besides slug).

In this example, we checked if the current query belongs to a category with a specific name (Uncategorized):

$categoryName = "Uncategorized";
if(is_category($categoryName)){
	//this page is the exact category page

}
else{
	//this page is NOT the specified category page
	//but it can be another category, or not a category page

}

Check if the current page is at least one of multiple categories

We are declaring an array for the is_category function. Using this array of values (IDs, Names, or Slugs), WordPress will check if the current page is at least one of the specified categories.

You can use mixed-type values for the array. It means each array value will be checked by the is_category function individually. It returns a true value when encountering the FIRST match.

In this example, is_category determines if the current page is a category page matching one of the given array values (ID, slug, and name mixed together):

$categories = array(7, 'my-category-slug', 'My Category Name');
if(is_category($categories)){
	//this page belongs to one of these categories

}
else{
	//this page does NOT belong to any of these categories
	//but it can be another category, or not a category page

}

What do you think about "How to Check if the Current Page is a Category in WordPress!"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu