Wordpress自动添加tag

2018-03-08  本文已影响151人  老胡聊聊天

网上找的代码,放到function.php里面即可,逻辑是从文章中把你用过的tag提取出来自动加为tag,省的自己再一个一个敲了。

加了tag的好处不言而喻,一个是keywords,第二是关联文章等等。

///* 自动为文章添加标签 */
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 ( strpos($post_content, $tag->name) !== false)
                wp_set_post_tags( $post_id, $tag->name, true );
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读