网站文章批量自动添加标签
[title]代码详情[/title]
有时候在更新文章的时候忘记给文章添加TAG,这样确实不利于这篇文章的内链契合度以及网站seo优化,手动添加标签当文章很多时太费时间,特别是一些喜欢采集文章的朋友,此代码亲测有效,代码添加到主题的functions.php里即可
[title]代码开始[/title]
//自动为文章添加标签
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 );
}
}
}
空空如也!