当前浏览:首页 / WordPress函数 / has_term()

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

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

has 判断有more...

term 分类项more...


描述

根据文章的term(分类法项目)的term_id、name和slug检查给定的term。以整数形式给出的term将仅根据文章的term的term_id进行检查。

如果未给定任何term,则确定文章是否存在任意的term。

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


参数

$term

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

默认值: ''

$taxonomy

(string) (可选) 分类法(taxonomy)名称。

默认值: ''

$post

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

默认值: null


返回

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



源码

查看源码 官方文档


更新日志

版本描述
3.1.0开始引入

使用示例

  • 示例1

    如果要检查文章中是否存在来自给定分类法(taxonomy)的任意项目(term),可以传入一个空字符串作为第一个参数。

    示例

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

    如果您希望有条件地显示某些仅在已将term添加到文章中时才适用的标记,则此选项非常有用。

  • 示例2

    示例

    if( has_term( 'jazz', 'genre' ) ) {
        // do something
    }
    
  • 示例3

    示例,用于检测文章是否存在所给定的自定义分类法中的项目,本例中,自定义分类法是“hook-type”,分类项是“action”

    $hook_css_class = '';
    if (has_term( 'action', 'hook-type' )) {
      $hook_css_class = "is-action-hook";
    } else {
      $hook_css_class = "is-filter-hook";
    };