返回介绍

has_tag()

发布于 2017-09-11 00:52:52 字数 2706 浏览 1064 评论 0 收藏 0

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

Check if the current post has any of given tags.


description

The given tags are checked against the post’s tags’ term_ids, names and slugs. Tags given as integers will only be checked against the post’s tags’ term_ids. If no tags are given, determines if post has any tags.

Prior to v2.7 of WordPress, tags given as integers would also be checked against the post’s tags’ names and slugs (in addition to term_ids) Prior to v2.7, this function could only be used in the WordPress Loop. As of 2.7, the function can be used anywhere if it is provided a post ID or post object.


参数

$tag

(string|int|array) (Optional) The tag name/term_id/slug or array of them to check for.

Default value: ''

$post

(int|object) (Optional) Post to check instead of the current post. (since 2.7.0)

Default value: null


返回值

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


源代码

File: wp-includes/category-template.php

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

更新日志

Versiondescription
2.6.0Introduced.

相关函数

Uses

  • wp-includes/category-template.php: has_term()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note Contributed by Andrea Castenetto

    If the article has tags, show them. Else do nothing. It works in the loop.

    
    if(has_tag()) {
        the_tags();
    } else {
        //Article untagged
    }
    
  2. If Post has tag, show them. Else if Post has category, show category. Otherwise do other.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文