in_category()WP 1.2.0

Checks whether the current (or specified) post belongs to the specified category (multiple categories can be specified). Conditional tag.

It checks the direct membership of the post to the category, i.e., whether the specified category contains the current/specified post. If, for example, the post belongs to a parent category or a child category of the specified one, the function will return false.

To check the relationship of the post to the category tree, use the custom function post_is_in_descendant_category() (example below).

The function can be used:

  • Inside the WordPress Loop.
  • Or outside the WP Loop, but only on a single post page - is_single().
  • Or it can be used anywhere if you specify which post needs to be checked.
1 time — 0.003672 sec (very slow) | 50000 times — 1.00 sec (very fast) | PHP 7.1.5, WP 4.8.1

No Hooks.

Returns

true|false. true if the condition is met and false if not.

Usage

if( in_category( $category, $post ) ){
	// ...
}
$category(string/array/number) (required)

ID, name, or slug of the category to check if the post belongs to it.

Multiple parameters can be specified in an array mixed together.

$post(number/object)
ID or object of the post. By default, the current post is automatically determined within the WordPress Loop or on the post page.

Examples

2

#1 Tree check for term membership

Let's check if the post is part of a custom taxonomy term (we'll also check the child terms to the specified one):

if(
	has_term( 11, 'taxonomy', $post->ID )
	||
	post_is_in_descendant_term( 11, 'taxonomy', $post->ID )
){
	// Current post in term 11 or its child term
}

Function post_is_in_descendant_term():

function post_is_in_descendant_term( $term_ids, $taxonomy = 'category', $post = null ){

	foreach( (array) $term_ids as $term_id ){

		$descendants = get_term_children( (int) $term_id, $taxonomy );

		if( $descendants && has_term( $descendants, $taxonomy , $post ) ){
			return true;
		}
	}

	return false;
}
1

#2 Checking the current post within the WordPress Loop.

This function is often used inside the Loop to check if the post belongs to the specified category, and if it does, to take some action:

if ( in_category( 'pachyderms' ) ) {
	// actions if the post belongs to the 'pachyderms' category
}
elseif ( in_category( [ 'Tropical Birds', 'small-mammals' ] ) ) {
	// actions if the post belongs to one of the categories 'Tropical Birds', 'small-mammals'
}
else {
	// if none of the previous conditions are met.
}

P.S. It is better to specify not the names, but the category IDs for verification

0

#3 Checking the current post outside the Loop.

On the post page (usually a single.php template file), the check can be done outside the Loop:

if ( in_category('fruit') ) {
	include 'single-fruit.php';
}
elseif ( in_category('vegetables') ) {
	include 'single-vegetables.php';
}
else {
	// The WordPress Cycle begins below
	if ( have_posts() ) : while ( have_posts() ) : the_post();
	// ...
}
0

#4 Check if the post belongs to the current or nested category

There are cases when we need to check if the post belongs to the category tree. For example, we check for category ID 60 and the post belongs to category 70, which is a child of category 60. But in_category() will return false in this case and sometimes we need it to return true.

In order to check if you belong to a category tree, you can sequentially specify all category names in the array:

if( in_category( [ 'Raspberries', 'Apples', 'Bananas', 'Pears', 'Plums' ] ) ) {
	// Action if the condition is met
}

This approach to validation is completely inflexible, because if we add a new subcategory to the "Fruit" category, we also need to add it to the array for validation as well.

To avoid such complications you can use such a check:

// Check if the post belongs to the "Fruit" category or any nested category.
if (
	in_category( 11 )
	||
	post_is_in_descendant_category( 11 )
) {
	// Here are all the "fruits"
}

Also, less desirable, but an option is to specify names:

$term = get_term_by( 'name', 'fruit', 'category' )

$is_in_descendant = false;
if( $term ){
	is_in_descendant = post_is_in_descendant_category( $term->term_id );
}

And here is the function post_is_in_descendant_category():

function post_is_in_descendant_category( $cats_ids, $_post = null ){

	foreach( (array) $cats_ids as $cat_id ){

		// get_term_children() accepts integer ID only
		$descendants = get_term_children( (int) $cat_id, 'category' );

		if( $descendants && in_category( $descendants, $_post ) ){
			return true;
		}
	}

	return false;
}

Use for checking if within a single cat:

post_is_in_descendant_category( 25 );

Use for checking if within an array of parent categories, or any of their subcats:

post_is_in_descendant_category( [ 25, 28, 124, 297, 298, 299 ] );

Changelog

Since 1.2.0 Introduced.
Since 2.7.0 The $post parameter was added.

in_category() code WP 6.9.1

function in_category( $category, $post = null ) {
	if ( empty( $category ) ) {
		return false;
	}

	return has_category( $category, $post );
}