wp_create_term()
云策文档标注
概述
wp_create_term() 函数用于向数据库添加新术语,如果该术语不存在。它首先检查术语是否存在,若存在则返回其ID,否则调用 wp_insert_term() 创建新术语。
关键要点
- 函数接受两个参数:必需的术语名称 $tag_name 和可选的分类法 $taxonomy(默认为 'post_tag')。
- 返回值是数组(包含术语ID等信息)或 WP_Error 对象。
- 内部逻辑:先调用 term_exists() 检查术语是否存在,若存在则返回其ID;否则调用 wp_insert_term() 插入新术语。
- 相关函数:wp_insert_term() 用于直接添加术语,term_exists() 用于检查术语存在性。
- 自 WordPress 2.8.0 版本引入。
原文内容
Adds a new term to the database if it does not already exist.
Parameters
$tag_namestringrequired-
The term name.
$taxonomystringoptional-
The taxonomy within which to create the term. Default
'post_tag'.
Source
function wp_create_term( $tag_name, $taxonomy = 'post_tag' ) {
$id = term_exists( $tag_name, $taxonomy );
if ( $id ) {
return $id;
}
return wp_insert_term( $tag_name, $taxonomy );
}
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |