WordPress为文章添加分类法函数
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);
扩展阅读
wp_set_post_terms()函数位于:wp-includes/post.php