自动将一组标签添加到新的wordpress帖子中



我有一个博客,每天都在那里发很多帖子。许多帖子使用了10个常用标签中的5个。与其每次都写出这些标签中的每一个,我宁愿取消选中一些我不需要的标签(然后添加任何新的或不常见的标签)。

所以,我想编辑我的functions.php文档,这样任何新帖子都会有一个包含10个标签的列表。如果标签已经存在,就不要做任何事情。如果可能的话,我想避免使用插件。

有人知道怎么做吗?这将非常有用。

代码看起来像:(注意:我的编程技巧非常糟糕,所以这可能是不对的,甚至是不可能的)

function default_tag_list() {
    if(new_post() && !get_the_tag_list()) { // using new_post() ... not sure how to check if its a new post.
        $default_tags = array('health', 'nutrition', 'diet', 'well-being', 'eating');
        return $default_tags; 
    }  // Then, somehow get the get_tag_list in the administration to use the default tags function (if it's a new post without any tags) ...
}

}

只需在函数中复制并粘贴以下代码即可。hp:

function my_data_update () {
    GLOBAL $post;
    wp_set_post_tags( $post->ID, 'health, nutrition, diet, well-being, eating', true );
}
add_action('publish_post', 'my_data_update');
add_action('save_post', 'my_data_update');

经过测试,这段代码一定能为您工作。

最新更新