WordPress纯代码实现自动锚文本优化版,优化了在文章里面如果没有添加锚文本不自动连接的bug。
意思就是你的文章里面只要出现了tag里有的关键词,都会自动锚文本。
未修改之前,锚文本只能是“wordpress”,
优化后,tag里有的关键词,都自动锚文本。
代码修改方法出自一位不愿意透露姓名的大神。
使用方法:
将下面的代码加入到主题的 functions.php 里即可。
/* 自动为文章内的标签添加内链开始 */ $match_num_from = 1; //每篇文章中的关键词数量低于多少则不描文本(请不要低于1) $match_num_to = 1; //同一篇文章中,同一个关键词最多描几次文本(这里是1次,建议不超过2次) add_filter('the_content','tag_link',1); function tag_sort($a, $b){ if ( $a->name == $b->name ) return 0; return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1; } function tag_link($content){ global $match_num_from,$match_num_to; $posttags = get_tags(); if ($posttags) { usort($posttags, "tag_sort"); foreach($posttags as $tag) { $link = get_tag_link($tag->term_id); $keyword = $tag->name; $cleankeyword = stripslashes($keyword); $url = "<a href="$link" title="".str_replace('%s',addcslashes($cleankeyword, '$'),__('View all posts in %s'))."""; $url .= ' target="_blank" class="tag_link"'; $url .= ">".addcslashes($cleankeyword, '$')."</a>"; $limit = rand($match_num_from,$match_num_to); $content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content); $content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content); $cleankeyword = preg_quote($cleankeyword,'''); $regEx = ''(?!((<.*?)|(<a.*?)))('. $cleankeyword .')(?!(([^<>]*?)>)|([^>]*?</a>))'s' . $case; $content = preg_replace($regEx,$url,$content,$limit); $content = str_replace( '%&&&&&%', stripslashes($ex_word),$content); } } return $content; } /*自动为文章内的标签添加内链结束 */
当然,如果嫌弃每个文章的tag连接的太多的话,可以试试显示每个文章最多的tag显示数:
if($num_id >= 4),代表每个文章最多显示4个tag,完美。建议不要太多tag,4个差不多了。
代码由 卡盟吧 修改优化哦。
/* 自动为文章内的标签添加内链开始 */ $match_num_from = 1; //每篇文章中的关键词数量低于多少则不描文本(请不要低于1) $match_num_to = 1; //同一篇文章中,同一个关键词最多描几次文本(这里是1次,建议不超过2次) $num_id = 0; //请不要修改这个参数 add_filter('the_content','tag_link',1); function tag_sort($a, $b){ if ( $a->name == $b->name ) { $num_id--; return 0; } return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1; } function tag_link($content){ global $match_num_from,$match_num_to; $posttags = get_tags(); if ($posttags) { usort($posttags, "tag_sort"); foreach($posttags as $tag) { if($num_id >= 4){ break; }else{ $num_id++; } $link = get_tag_link($tag->term_id); $keyword = $tag->name; $cleankeyword = stripslashes($keyword); $url = "<a href="$link" title="".str_replace('%s',addcslashes($cleankeyword, '$'),__('View all posts in %s'))."""; $url .= ' target="_blank" class="tag_link"'; $url .= ">".addcslashes($cleankeyword, '$')."</a>"; $limit = rand($match_num_from,$match_num_to); $content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content); $content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content); $cleankeyword = preg_quote($cleankeyword,'''); $regEx = ''(?!((<.*?)|(<a.*?)))('. $cleankeyword .')(?!(([^<>]*?)>)|([^>]*?</a>))'s' . $case; $content = preg_replace($regEx,$url,$content,$limit); $content = str_replace( '%&&&&&%', stripslashes($ex_word),$content); } } return $content; } /*自动为文章内的标签添加内链结束 */
未优化版,也就是文章有tag才会锚文本:
//自动连接关键字 $match_num_from = 1; //一篇文章中同一个关键字少于多少不秒文本(这个直接填1就好了) $match_num_to = 1; //一篇文章中同一个关键字最多出现多少次描文本(建议不超过2次) //连接到WordPress的模块 add_filter('the_content','tag_link',1); //按长度排序 function tag_sort($a, $b){ if ( $a->name == $b->name ) return 0; return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1; } //改变标签关键字 function tag_link($content){ global $match_num_from,$match_num_to; $posttags = get_the_tags(); if ($posttags) { usort($posttags, "tag_sort"); foreach($posttags as $tag) { $link = get_tag_link($tag->term_id); $keyword = $tag->name; //连接代码 $cleankeyword = stripslashes($keyword); $url = "<a href="$link" title="".str_replace('%s',addcslashes($cleankeyword, '$'),__('View all posts in %s'))."""; $url .= ' target="_blank"'; $url .= ">".addcslashes($cleankeyword, '$')."</a>"; $limit = rand($match_num_from,$match_num_to); //不连接的 代码 $content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content); $content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content); $cleankeyword = preg_quote($cleankeyword,'''); $regEx = ''(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))'s' . $case; $content = preg_replace($regEx,$url,$content,$limit); $content = str_replace( '%&&&&&%', stripslashes($ex_word), $content); } } return $content; } add_filter( 'pre_option_link_manager_enabled', '__return_true' );
下面代码是正确的:
/*自动为文章内的标签添加内链开始*/ // 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++; } } } } //自动连接关键字 $match_num_from = 1; //一篇文章中同一个关键字少于多少不秒文本(这个直接填1就好了) $match_num_to = 1; //一篇文章中同一个关键字最多出现多少次描文本(建议不超过2次) //连接到WordPress的模块 add_filter('the_content','tag_link',1); //按长度排序 function tag_sort($a, $b){ if ( $a->name == $b->name ) return 0; return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1; } //改变标签关键字 function tag_link($content){ global $match_num_from,$match_num_to; $posttags = get_the_tags(); if ($posttags) { usort($posttags, "tag_sort"); foreach($posttags as $tag) { $link = get_tag_link($tag->term_id); $keyword = $tag->name; //连接代码 $cleankeyword = stripslashes($keyword); $url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('View all posts in %s'))."\""; $url .= ' target="_blank"'; $url .= ">".addcslashes($cleankeyword, '$')."</a>"; $limit = rand($match_num_from,$match_num_to); //不连接的 代码 $content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content); $content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content); $cleankeyword = preg_quote($cleankeyword,'\''); $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case; $content = preg_replace($regEx,$url,$content,$limit); $content = str_replace( '%&&&&&%', stripslashes($ex_word), $content); } } return $content; } add_filter( 'pre_option_link_manager_enabled', '__return_true' ); /*自动为文章内的标签添加内链结束 */