我知道这似乎是一个简单的操作,但我找不到任何资源或文档来解释如何使用帖子 ID 以编程方式向帖子添加和删除标签。
下面是我正在使用的示例,但它似乎覆盖了所有其他标签......
function addTerm($id, $tax, $term) {
$term_id = is_term($term);
$term_id = intval($term_id);
if (!$term_id) {
$term_id = wp_insert_term($term, $tax);
$term_id = $term_id['term_id'];
$term_id = intval($term_id);
}
$result = wp_set_object_terms($id, array($term_id), $tax, FALSE);
return $result;
}
你需要先打电话获取对象术语 http://codex.wordpress.org/Function_Reference/wp_get_object_terms获取所有已经存在的术语。
更新了代码
function addTerm($id, $tax, $term) {
$term_id = is_term($term);
$term_id = intval($term_id);
if (!$term_id) {
$term_id = wp_insert_term($term, $tax);
$term_id = $term_id['term_id'];
$term_id = intval($term_id);
}
// get the list of terms already on this object:
$terms = wp_get_object_terms($id, $tax)
$terms[] = $term_id;
$result = wp_set_object_terms($id, $terms, $tax, FALSE);
return $result;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)