in_category()WP 1.2.0

Check if the current post is within any of the given categories.

The given categories are checked against the post's categories' term_ids, names and slugs. Categories given as integers will only be checked against the post's categories' term_ids.

Prior to v2.5 of WordPress, category names were not supported. Prior to v2.7, category slugs were not supported. Prior to v2.7, only one category could be compared: in_category( $single_category ). Prior to v2.7, this function could only be used in the WordPress Loop. As of 2.7, the function can be used anywhere if it is provided a post ID or post object.

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

1 time — 0.003672 sec (very slow) | 50000 times — 1.00 sec (very fast) | PHP 7.1.5, WP 4.8.1

No Hooks.

Return

true|false. True if the current post is in any of the given categories.

Usage

in_category( $category, $post );
$category(int|string|int[]|string[]) (required)
Category ID, name, slug, or array of such to check against.
$post(int|WP_Post)
Post to check.
Default: current post

Examples

0

#1 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

#2 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

#3 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 ] );
0

#4 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;
}

Changelog

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

in_category() code WP 6.4.3

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

	return has_category( $category, $post );
}