Warning: Trying to access array offset on false in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 216
has_tag() - 检查当前文章是否有给定的标签 - WordPress函数 - WordPress动力
当前浏览:首页 / WordPress函数 / has_tag()

has_tag( string|int|array $tag = '', int|object $post = null )

检查当前文章是否有给定的标签


Warning: Undefined array key -1 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 185

Warning: Undefined array key 2 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 186

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "68cbf86a6adef9731b41f24cf8261f7c" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "a935db3d255e17450f4b387b9565b626" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

has 判断有more...

tagmore...


描述

根据文章标签的term_id、name和slug检查给定的标签。给定整数形式的标签只会根据文章标签的term_id进行检查。

如果没有给出标签,则确定文章是否有任意标签。

更多类似的主题函数信息,请查看主题开发手册中的条件标签文章。


参数

$tag

(string|int|array) (可选) 要检查的标签name/term_id/slug,或它们的数组。

默认值: ''

$post

(int|object) (可选) 要检查的文章,而不是当前文章。

默认值: null


返回

(bool) 如果当前文章有给定的标签(或不给定,而存在任意标签),则为true。否则为false。



源码

查看源码 官方文档


更新日志

版本描述
2.7.0如果提供了$post,则可以在WordPress循环之外使用
2.6.0开始引入

使用示例

  • 示例1

    如果文章有标签,显示它们。否则什么也不做。它在循环中工作。

    if(has_tag()) {
        the_tags();
    } else {
        //Article untagged
    }
    
  • 示例2

    如果文章有标签,则显示它们。否则,如果文章有类别,则显示类别。否则,执行其他操作。

    if(has_tag()) {
    
          the_tags(); //show tags
    
    } elseif(has_category()) {
    
          the_category(); //show category
    
    } else {
          //do something different
    }