一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

wordpress给文章自动添加网站tag标签

时间:2014-03-04 编辑:简简单单 来源:一聚教程网

以下代码就可以解决,它会在你发布/保存文章时,检测文章的内容中,是否出现曾经使用过的标签,如果出现,就自动为文章添加这些标签。

 代码如下 复制代码

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 );
}
}
}

把代码放到functions.php即可,是不是很方便呢?

热门栏目