测速网一直在寻找wordpress优化的最佳解决方案,因为每个客户都在问对于标签这个功能是否可以自动添加或是自己加上链接,今天测速网给大家分享一段出自tag优化插件的seo代码使用便是自动为新文章添加已经使用过的标签。这款功能可以说是一劳永逸,只要是之前有添加过的标签,在新建的文章保存文章时就会自动添加曾经使用过的标签,是不是很方便?
将此段代码复制到主题的 functions.php 中即可使用:(测速网的所有主题已经同步此功能:)
area data-settings="dblclick" readonly >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 );}}}area>16add_action('save_post', 'auto_add_tags');function auto_add_tags(){$tags = get_tags( array('hide_empty' => false) );$post_id = get_the_ID();$post_co
ntent = 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 );}}}
提示:添加本段代码后,新建的文章才能检测文章内的标签内容,已经发布的是不能自动检测的,需要手动更新下文章内容。