代码详情
有时候在更新文章的时候忘记给文章添加TAG,这样确实不利于这篇文章的内链契合度以及网站seo优化,手动添加标签当文章很多时太费时间,特别是一些喜欢采集文章的朋友,此代码亲测有效,代码添加到主题的functions.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 ( strpos($post_content, $tag->name) !== false)
wp_set_post_tags( $post_id, $tag->name, true );
}
}
}
- THE END -
最后修改:2022年10月18日
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:https://i.likejay.cn/312.html