WordPress at a glance

is_tax() WP 2.5.0

Works on the custom taxonomy archive page. Conditional tag.

Returns true if a new taxonomy is registered and the user has visited the page of this taxonomy element (term).

It is similar to is_category(), is_tag(), but does not replace them. It works only with new custom taxonomies. I.e. is_tax() returns false on the category or tag archive page!

Returns false if the query_var parameter is not defined for the taxonomy while registering the taxonomy. See: register_taxonomy().

If the $taxonomy parameter is specified, this function will additionally check if the query is for that specific $taxonomy.

If the $term parameter is specified in addition to the $taxonomy parameter, this function will additionally check if the query is for one of the terms specified.

If you want to check whether the post is in a taxonomy, use has_term()

Notes:

  • Global. WP_Query. $wp_query Global WP_Query instance.
Works based on: WP_Query::is_tax()
✈ 1 time = 0.000017s = very fast | 50000 times = 0.02s = speed of light PHP 7.0.2, WP 4.4.2

No Hooks.

Return

true/false. True for custom taxonomy archive pages, false for built-in taxonomies (category and tag archives).

Usage

if( is_tax( $taxonomy, $term ) ){
	// it is taxonomy...
}
$taxonomy(string/array)
Taxonomy slug or slugs. You can specify multiple taxonomies in an array.
Default: ''
$term(int/string/array)
Term ID, name, slug or array of Term IDs, names, and slugs.
Default: ''

Examples

#1 Check that we are on a taxonomy archive page

Below are some examples of when the is_tax() function returns true (works).

is_tax();
// When any custom taxonomy archive page is being displayed.

is_tax( 'channel' );
// When the archive page for taxonomy of 'channel' is being displayed.

is_tax( 'channel', 'BBC1' );
// When the archive page for taxonomy of 'channel' is being displayed and the 'channel' taxonomy term is 'BBC1'.

is_tax( 'channel', array( 'BBC1', 'SNN' ) );
// When the archive page for taxonomy of 'channel' is being displayed and the 'channel' taxonomy term is 'BBC1' or 'SNN'.

#2 Specificity with "Post Format" taxonomy

Post formats in WP are implemented through taxonomy. However, the slug of "Post Format" taxonomy element is not used as it is used in other taxes: the post-format- prefix is added to it.

For example, there is a post format "Aside" with the slug "aside". Its name will be post-format-aside, but not simple aside.

if( is_tax( 'post_format' ) ){
	// archive page for any "Post Format".
}

if( is_tax( 'post_format', 'post-format-aside' ) ){
	// archive page for 'aside' "Post Format".
}

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

<?php
function is_tax( $taxonomy = '', $term = '' ) {
	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_tax( $taxonomy, $term );
}

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