WordPress at a glance

has_tag() WP 2.6.0

Checks if the current post has any of the given tags. A conditional tag.

The given in $tag parameter tags are checked against the post's tags — against: term_ids, names, or slugs. If only integers are passed it will be checked against the post's tags IDs (term_ids).

If no tags are given, determines if the post has any tag.

Works based on: has_term()
✈ 1 time = 0.001853s = very slow | 50000 times = 96.58s = very slow

No Hooks.

Return

true/false. True if the current post has any of the given tags (or any tag, if no tag specified).

Usage

has_tag( $tag, $post );
$tag(string/int/array)
The tag name/term_id/slug or array of them to check for.
Default: ''
$post(int/object)
Post to check instead of the current post.
Default: null

Examples

#1 Check if the current post has any tags

<?php 
if( has_tag() )
	echo 'The current post has tags!';
?>

#2 Check if the post has kino tag

<?php
if( has_tag( 'kino' ) )
	echo 'The current post has tag `kino`';
?>

#3 Check if the post 56 has tags 49 and 89 (you can use their names or slugs instead of the ids)

<?php
if( has_tag( [45,89], 56 ) )
	echo 'Post 56 (ID) has tags ID 45 and 89';
?>

Changelog

Since 2.6.0 Introduced.

Code of has tag: wp-includes/category-template.php WP 5.2.4

<?php
function has_tag( $tag = '', $post = null ) {
	return has_term( $tag, 'post_tag', $post );
}

Related Functions

From tag: Conditional tags (all)

More from category: For posts

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register