• 欢迎访问刘浪seo网站,跨境电商俱乐部 QQ群262187934-一个无私分享亚马逊干货的群,一群年轻活力的跨境电商人!

给wordpress文章自动添加标签内链锚文本

SEO算法和思维 Charles.z 8年前 (2016-03-23) 3739次浏览 0个评论

内链作为 seo 优化的一部分,合理的关键词锚文本可以提升核心关键词的权重,在布局核心词锚文本内链的时候一般使用手工操作,如果信息量大的网站,要把编辑累成狗了。当然也可以直接操作数据库进行添加,但是数据库操作需要一定的技术能力,也存在一定的风险。这里提供一个在 function.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_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" 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 标签加入文章中包含这个标签文字的内容形成锚文本,如果觉得过于复杂,可以使用 WP keyword link plugin 插件来达到这个目的。


刘浪 BLOG , 版权所有丨如未注明 , 均为原创
喜欢 (0)
[13362309299]
分享 (0)
Charles.z
关于作者:

您必须 登录 才能发表评论!