加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – WordPress API:添加/删除帖子上的标签

发布时间:2020-12-13 14:00:33 所属栏目:PHP教程 来源:网络整理
导读:我知道这似乎是一个简单的操作,但是我找不到任何资源或文档来解释如何以编程方式使用帖子ID添加和删除标签. 以下是我正在使用的示例,但似乎覆盖了所有其他标签… function addTerm($id,$tax,$term) { $term_id = is_term($term); $term_id = intval($term_id
我知道这似乎是一个简单的操作,但是我找不到任何资源或文档来解释如何以编程方式使用帖子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),FALSE);

    return $result;
}
您需要首先致电 get_object_terms才能获得已经存在的所有条款.

更新代码

function addTerm($id,$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,FALSE);

    return $result;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读