我们为什么要自动给WordPress文章标签锚文本呢?答案是不可置疑的,当然是为了SEO的优化。锚文本在网站的SEO优化中占据着很大的比重,合理的关键词锚文本可以给网站的排名带来很大的帮助,大多数建站系统在布局文章关键的锚文本时都要手动去更改、去更新。这样的工作量是非常庞大的,而且效率极其低下。有些聪明的站长可能会利用执行数据库语言来批量的更换关键词的锚文本,对于菜鸟来说数据库操作可能太难,所以就有了这篇教程。

functions.php加入代码

/*
自动给WordPress文章标签锚文本
http://www.wpxzt.com
*/
$match_num_from = 1; //一篇文章中同一个标签少于几次不自动链接
$match_num_to = 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 = "";
                                $limit = rand($match_num_from,$match_num_to);
                                $content = preg_replace( '|(]+>)(.*)('.$ex_word.')(.*)(]*>)|U'.$case, '$1$2wordpress主题之家$4$5', $content);
                                $content = preg_replace( '|()|U'.$case, '$1$2wordpress主题之家$4$5', $content);
                                $cleankeyword = preg_quote($cleankeyword,'\'');
                                $regEx = '\'(?!((<.*?)|(]*?)>)|([^>]*?))\'s' . $case;
                                $content = preg_replace($regEx,$url,$content,$limit);
                                $content = str_replace( 'wordpress主题之家', stripslashes($ex_word), $content);
                        }
                }
        return $content;
}
add_filter('the_content','tag_link',1);

这样以来,就可以自动给WordPress文章标签锚文本。

参与评论