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);

扩展阅读

wp_set_post_terms()函数位于:wp-includes/post.php

相关函数:

原创文章,作者:,如若转载,请注明出处:https://ce.771633.xyz/1901.html

Like (0)
Previous 2025年3月8日
Next 2025年3月8日

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注