Warning: Trying to access array offset on false in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 216
term_exists() - 确定分类项(term)是否存在 - WordPress函数 - WordPress动力
当前浏览:首页 / WordPress函数 / term_exists()

term_exists( int|string $term, string $taxonomy = '', int $parent = null )

确定分类项(term)是否存在


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 "2df702d60daa556bbc409461c8106fdb" 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 "90276a70c1ce682ec49779101f60796b" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

exists 判断存在

term 分类项more...


描述

以前为is_term(),在2.3.0中引入。

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


参数

$term

(int|string) (必填) 要检查的term。接受term ID、slug或name。

$taxonomy

(string) (可选) 要使用的分类名称(taxonomy name)。

默认值: ''

$parent

(int) (可选) 分类项(term)父级ID,以此限制搜索范围。

默认值: null


返回

(mixed) 如果该分类项(term)不存在,则返回null。如果没有指定分类法(taxonomy)并且分类项ID存在,则返回该ID。如果指定了分类法并且存在配对的分类项,则返回分类项ID和分类法ID的数组。如果分类项ID传递为0,则返回0。



源码

查看源码 官方文档


更新日志

版本描述
6.0.0转换为使用get_terms()
3.0.0开始引入

使用示例

  • 示例1

    返回的数组:

    ['term_id' => ID, 'term_taxonomy_id' => ID]

    返回ID的字符串,而不是int。如果需要int值,则需要转换它。

  • 示例2

    检查“Uncategorized”类别是否存在
    注意:term_exists()运行数据库查询。get_term()可以用于相同的目的,但它使用term缓存。

    $term = term_exists( 'Uncategorized', 'category' );
    if ( $term !== 0 && $term !== null ) {
    	echo __( "'Uncategorized' category exists!", "textdomain" );
    }
    
  • 示例3

    检查“Untagged” post_tag 类别是否存在
    注意:term_exists()运行数据库查询。get_term()可以用于相同的目的,但它使用term缓存。

    $term = term_exists( 'Untagged', 'post_tag' );
    if ( $term !== 0 && $term !== null ) {
    	echo __( "'Untagged' post_tag exists!", "textdomain" );
    }