WordPress at Your Fingertips

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 visited the page of the element of this taxonomy (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 taxonomy is not visible on front-end — the query_var parameter is set to false 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.

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

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

Used By: is_product_tag()
1 time — 0.000017 sec (very fast) | 50000 times — 0.02 sec (speed of light) | PHP 7.0.2, WP 4.4.2

No Hooks.

Return

true|false. Whether the query is for an existing custom taxonomy archive page. 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".
}

Notes

  • Global. WP_Query. $wp_query WordPress Query object.

Changelog

Since 2.5.0 Introduced.

Code of is_tax() WP 5.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 );
}

From tag: Conditional tags (page type and request)

More from tag: Conditional tags (all)

More from category: Queries

More from Template Tags: Main Functions

vladlu 100vlad.lu
Editors: Kama 100
No comments
    Log In