WordPress at a glance

is_category() WP 1.5.0

Conditional tag. Checks whether the category page is displayed or not.

It is a Boolean function, i.e. this function returns a true if the condition matches and false if not.

If the $category parameter is specified, this function will additionally check if the query is for one of the categories specified.

Notes:

  • Global. WP_Query. $wp_query Global WP_Query instance.
Works based on: WP_Query::is_category()
✈ 1 time = 0.000013s = very fast | 50000 times = 0.01s = speed of light PHP 7.0.2, WP 4.4.1

No Hooks.

Return

true/false.

Usage

if( is_category( $category ) ){
	// code...
}
$category(string/number/array)
Category ID, name, slug, or array of Category IDs, names, and slugs.
Default: ''

Examples

#1 An example of different types of checks

is_category();
// the function returns true if any category page is displayed.

is_category( '9' );
is_category( 9 );
// true if the category page with ID 9 is displayed.

is_category( 'Out there' );
// true if the category with the name 'Out there'.

is_category( 'out-there' );
// true if a category named 'out-there' is shown.

is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) );
// Returns true if the category with ID 9 is shown or with name  
// 'blue-cheese', or with slug 'Stinky Cheeses'.

Code of is_category: wp-includes/query.php VER 4.9.8

<?php
function is_category( $category = '' ) {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
		return false;
	}

	return $wp_query->is_category( $category );
}

Related Functions

From tag: Conditional tags (page type and request)

More from tag: Conditional tags (all)

More from category: Queries

More from Template Tags: Main Functions

No comments
    Hello, !     Log In . Register