top
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!邮件:766378891@qq.com

WordPress 纯代码实现自动添加标签


共计 415 个字符,预计需要花费 2 分钟才能阅读完成。

如果不想手动添加标签,可以纯代码实现自动添加标签,不过只能添加已存在的标签。

function.php 添加如下代码:

add_action('save_post', 'auto_add_tags');

function auto_add_tags() {
    $tags = get_tags(array('hide_empty' => false));
    $post_id = get_the_ID();
    $post_content = get_post($post_id)->post_content;

    if ($tags) {
        foreach ($tags as $tag) {
            // If the tag is already used in the post content, add it to the post tags
            if (strpos($post_content, $tag->name) !== false) {
                wp_set_post_tags($post_id, $tag->name, true);
            }
        }
    }
}

提醒:本文发布于690天前,文中所关联的信息可能已发生改变,请知悉!

Tips:清朝云网络工作室

THE END
icon
0
icon
分享
icon
二维码
icon
海报
发表评论
评论列表

赶快来坐沙发

您好,今天过得怎么样?