探索建站和运维乐趣

WordPress代码替代插件实现关键字内链 提高站内权重

如果我们站长有比较重视网站SEO优化效果的,应该知道站内内链的重要性,我们可能在撰写文章的时候会手动添加当前网站中的内链至其他文章页面或者特定的关键字。有没有更好的办法自动化实现部分的特有关键字内链效果呢?当然是有的,在这篇文章中我们可以使用无插件实现文章Tag关键字实现内链效果。

也就是说,如果我们使用这个功能之后,我们只需要在当前文章中添加标签/关键字/Tag,就可以自动检索当前文章中出现的关键字实现内链。

/* 自动Tag标签关键字内链 Edit By Laobuluo.com*/
$match_num_from = 1; // 同一个标签少于几次不自动链接
$match_num_to = 4; // 一个关键字最多内链几次
function tag_sort($a, $b){
if ( $a->name == $b->name ) return 0;
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}
function tin_tag_link($content){
global $match_num_from,$match_num_to;
$posttags = get_the_tags();
if ($posttags) {
usort($posttags, "tag_sort");
$ex_word = '';
$case = '';
foreach($posttags as $tag) {
$link = get_tag_link($tag->term_id);
$keyword = $tag->name;
$cleankeyword = stripslashes($keyword);
$url = "<a href=\"$link\" class=\"tooltip-trigger tin\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('查看更多关于 %s 的文章'))."\"";
$url .= ' target="_blank"';
$url .= ">".addcslashes($cleankeyword, '$')."</a>";
$limit = rand($match_num_from,$match_num_to);
$content = preg_replace( '|(<a[^>]+>)(.*)<pre.*?>('.$ex_word.')(.*)<\/pre>(</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('the_content','tin_tag_link',12);

将当前脚本添加到当前主题的Functions.php文件中。我们可以根据实际设置match_num_from、match_num_to,或者我们就默认即可。

赞(0)
转载保留:老部落 » WordPress代码替代插件实现关键字内链 提高站内权重


关注公众号『老蒋朋友圈』

获取更多建站运营运维新知!
互联网创业、前沿技术......