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

本站正在使用的主机

本站目前托管于腾讯云香港轻量服务器,价格便宜访问速度也快,使用本站推荐链接购买还可免费获得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

相关函数:

背字根微信二维码

本站文章均为原创,码字非常不容易,转载请注明原文出处,给苦逼的作者保留一点创作动力。