返回介绍

has_term()

发布于 2017-09-11 00:53:11 字数 3104 浏览 1032 评论 0 收藏 0

has_term( string|int|array $term = '',  string $taxonomy = '',  int|object $post = null )

Check if the current post has any of given terms.


description

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


参数

$term

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

Default value: ''

$taxonomy

(string) (Optional) Taxonomy name

Default value: ''

$post

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

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_term( $term = '', $taxonomy = '', $post = null ) {
	$post = get_post($post);

	if ( !$post )
		return false;

	$r = is_object_in_term( $post->ID, $taxonomy, $term );
	if ( is_wp_error( $r ) )
		return false;

	return $r;
}

更新日志

Versiondescription
3.1.0Introduced.

相关函数

Uses

  • wp-includes/taxonomy.php: is_object_in_term()
  • wp-includes/post.php: get_post()
  • wp-includes/load.php: is_wp_error()

Used By

  • wp-includes/category-template.php: has_category()
  • wp-includes/category-template.php: has_tag()
  • wp-includes/canonical.php: redirect_canonical()
  • wp-includes/post-formats.php: has_post_format()

User Contributed Notes

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

    Example

    
    if( has_term( 'jazz', 'genre' ) ) {
        // do something
    }
    
  2. If you’re checking for the presence of any terms from a given taxonomy on a post, you can pass in an empty string as the first parameter.

    Example

    
    if( has_term('', 'genre') ){
    	// do something
    }
    

    This is useful if you want to conditionally display some markup that applies only if terms have been added to a post.

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

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

发布评论

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