简体   繁体   English

以编程方式将标签添加到Wordpress帖子而不覆盖预先存在的标签

[英]Programmatically Add Tag to Wordpress Post Without Overwriting Pre-Existing Tags

I am able to add a tag to my post in code, but it overwrites the existing array of tags. 我可以在代码中为帖子添加标签,但它会覆盖现有的标签数组。 Does anyone know of a way to add a tag to the array of pre-existing tags so that none are erased? 有没有人知道如何将标记添加到预先存在的标记数组中,以便不会删除任何标记?

wp_set_object_terms($post_id, 'mytagname', 'post_tag');

This part of the Wordpress API seems a bit clunky, but maybe I'm completely missing something. 这部分Wordpress API似乎有点笨拙,但也许我完全错过了一些东西。 Any help would be appreciated! 任何帮助,将不胜感激!

Turns out RTFM was the answer. 原来RTFM就是答案。

Look at the fourth parameter of the function - $append . 查看函数的第四个参数 - $append If it is set to true , the tags are appended to the existing tags, if it's false (the default) they replace them. 如果设置为true ,则标记将附加到现有标记,如果它是false (默认值),则替换它们。

So you want to call it like so: 所以你想这样称呼它:

wp_set_object_terms($post_id, 'mytagname', 'post_tag', true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM