How to Check if the Current Page is a Tag in WP (+Example)

2.8333333333333 from 5
from 12 user
(Rate this post)
How to Check if the Current Page is a Tag in WP (+Example)

If the current page is a tag page in WordPress, the is_tag function will return a true value. The is_tag function accepts only one parameter. you can use that parameter to check if the current page is a specific tag. We're going to review the is_tag function and its usage in an example.

Check if the current page is a tag page, using the is_tag function

Tag is one of WordPress’s default taxonomies, like categories, post formats, and many other classifications.

When a user visits an archive page, developers can detect if that page is a tag page by using the is_tag function of WordPress.

is_tag function syntax

The syntax has everything you need to know about what it does and what it needs to get the information:

is_tag(int|string|int[]|string[] $tag = '')

$tag parameter is optional; if you do not specify it, is_tag() will return true for all tag pages.

You can check if the current page is a specified tag by declaring the target tag with ID, Name, or slug.

is_tag function also can check if one page belongs to any of multiple declared targets with an array of IDs, Names, and slugs.

Keep reading for examples…

Check if the current page is a tag page (not specified)

To check if a page is a tag page (not specified tag, just tag page in general), you can just use this function in a condition as it is:

if(is_tag()){
	echo "This page is a tag page in general";
}

Check if the current page is a SPECIFIED tag

is_tag function accepts Integer for tag ID and String for tag name or slug.

in the next three examples, we’ll check the current page for a specified tag with ID, name, and slug:

Using is_tag with tag id:

Because tags are WP terms, tag id is the term_id of a tag (Integer).

In this example, We’ll check if the current page in the user’s browser is the tag with term_id = 5:

$targetTagId = 5; //Tag with term_id = 5
if(is_tag($targetTagId)){
	//this page is the exact tag

}
else{
	//this page is NOT the specified tag page
	//but it can be another tag, or not a tag page

}

Using is_tag with tag name:

is_tag function accepts a string to check the page for a tag with specified name (or slug).

$targetTagName = "My Tag"; //"My Tag" as tag name
if(is_tag($targetTagName)){
	//this page is the exact tag

}
else{
	//this page is NOT the specified tag page
	//but it can be another tag, or not a tag page

}

Using is_tag with tag slug:

Slug is a little different from the name. Slug does not contain any spaces or capital letters in it.

In this example, we’ll check if the current page does blog to a tag with the “my-tag” slug:

$targetTagSlug = "my-tag"; //"my-tag" as tag slug
if(is_tag($targetTagSlug)){
	//this page is the exact tag

}
else{
	//this page is NOT the specified tag page
	//but it can be another tag, or not a tag page

}

Check if the current page belongs to one of the multiple tags

WordPress developers can determine an array of mixed-type values to check if the current page belongs to one of them or not.

$targetTags = array(1, 'my-tag', 'My Tag');
if(is_tag($targetTags)){
	//this page belongs to one of these tags

}
else{
	//this page does NOT belong to any of these tags
	//but it can be another tag, or not a tag page

}

What do you think about "How to Check if the Current Page is a Tag in WP (+Example)"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu