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

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

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

/* 自动Tag标签关键字内链 壹资源网:www.iezyw.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,或者我们就默认即可。

免责声明

1. 本站所有资源来源于网络,如有侵权请联系站长!

2. 如有链接失效或无法下载,请联系本站站长处理!

3. 如遇到解压密码不正确,无法解压的请联系本站站长处理!

4. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!

5. 不得使用于非法商业用途,不得违反国家法律。否则一切后果自负!

6. 本站所收取的费用仅是收集整理费用,并非售卖费用,版权争议与本站无关!

7. 本站提供的源码、模板、插件、软件等其他资源,都不包含技术服务请大家谅解!

8. 本站站长邮箱:[email protected]