WordPress为文章添加分类法函数:wp_set_post_terms

WordPress函数wp_set_post_terms为指定文章添加分类法。

wp_set_post_terms( int $post_id, string|array $tags = '', string $taxonomy = 'post_tag', bool $append = false )

函数参数

$post_id

整数,文章的ID

$tags

字符串或数组

分类/标签的名称

$taxonomy

字符串,默认值:post_tag

分类法名称,可选值:category

$append

布尔值,默认值:false

默认下,将删除旧分类/标签再添加新分类/标签。如果值为true,则不删除旧分类/标签。

函数使用示例

$exchange = term_exists($this->response['exchangeShortName'], 'exchange');
if(!$exchange) {
    $exchange = wp_insert_term($this->response['exchange'], 'exchange', array('slug' => $this->response['exchangeShortName']));
}
wp_set_post_terms($this->postID, array($exchange['term_id'] ), 'exchange', false);
wp_set_post_terms($this->postID, array((int) $exchange['term_id'] ), 'exchange', false);
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » WordPress为文章添加分类法函数:wp_set_post_terms