wordpress每次都要手动给文章添加标签,很麻烦?不知文章是否出现以前用过的标签,怎么办?以下代码就可以解决这些问题,它会在你发布/保存文章时,检测文章的内容中,是否出现曾经使用过的标签,如果出现,就自动为文章添加这些标签。
将代码添加到wordpress主题的 functions.php 即可:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 /**
* WordPress 自动为文章添加已使用过的标签
* http://www.jianyu87.com/2013102068.html
*/
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 );
}
}
}
相关日志:
- 上一篇:2013年10月
- 下一篇:58同城都在纽约上市了,我们还有理由不加倍努力吗?
让WordPress 自动为新文章添加已使用过的标签:等您坐沙发呢!