粘贴以下代码 function.php 在中间,可以实现免插件 WordPress 文章自动添加标签:
// WordPress 已使用的标签自动添加到文章中function array2object($array) { // 数组转对象 if (is_array($array)) { $obj = new StdClass(); foreach ($array as $key => $val){$obj->$key = $val; } } else { $obj = $array; } return $obj;}function object2array($object) { // 对象转数组 if (is_object($object)) { foreach ($object as $key => $value) {$array[$key] = $value; } } else { $array = $object; } return $array;}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) { $i = 0; $arrs = object2array($tags);shuffle($arrs);$tags = array2object($arrs);// 打乱顺序 foreach ( $tags as $tag ) { // 如果使用过的标签出现在文章内容中,自动添加这些标签if ( strpos($post_content, $tag->name) !== false){ if ($i == 5) { // 控制输出量break; } wp_set_post_tags( $post_id, $tag->name, true ); $i ;} } }}