今天给大家分享一个WordPress SEO优化小技巧,该技巧能够实现文章内的关键词标签自动添加内链。
内链的好外,自然就不用说了,它可以让百度蜘蛛在你的网站爬行更多的地方。
当然,很多WordPress插件也可以实现这个功能,但是今天图酷给大家分享的是不用插件给文章关键词标签自动添加内链的方法。
本站亲测

在开发wordpress 主题的时候,作为wordpress开发者,应该考虑到让wordpress主题有利于SEO优化。这些,我们在前面也介绍了一些,如:给首页添加关键词、描述,wordpress文章图片自动添加ALT属性…… 本章,我们再来介绍另外一种,给wordpress主题自动添加关键词内链,也就是说,让文章中的关键词或Tag标签自动内链。内链的好外,我就不用说了,可以让百度蜘蛛在你的网站爬行更多的地方。当然,我们可以通过插件来实现关键词内链,这方面的插件非常。这不是本章所要介绍的。本章的重点是:wordpress主题通过代码来添加关键词内链。
那怎么如何让WordPress站点的文章关键词标签自动添加内链呢?其实我们只需要在主题目录下的functions.php文件中添加一段代码就可以实现了。
实用方法
- 1.在主题目录下的functions.php文件中添加下方代码就可以实现了WordPress给文章关键词标签自动添加内链。
//WordPress文章关键词自动内链
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){
$match_num_from = 1; //一个标签少于几次不链接
$match_num_to = 5; //一个标签最多链接几次
$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, '$'),__('更多关于 %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','tag_link',1);
//自动为文章添加标签
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) {
foreach ( $tags as $tag ) {
// 如果文章内容出现了已使用过的标签,自动添加这些标签
if ( strpos($post_content, $tag->name) !== false)
wp_set_post_tags( $post_id, $tag->name, true );
}
}
}
通过上面的代码,可以实现Tag标签在wordpress文章中第一次出现的时候添加内链。当然,你也可以让Tag标签在wordpress文章中出现多次内链,只需要修改这段代码中的开头2句,修改它们的后面数字即可。
2、wordpress文章内链指定关键词。
有时,我们希望wordpress文章能有指定关键词指向首页或其它我们重点要推广的页面。这时,我们可以给wordpress主题添加指定关键词内链。代码如下:
//指定关键词内链
function content_keywords_link($text){
$replace = array(
'图酷优设' => '<a href="https://www.xtuku.com/" rel="bookmark" title="图酷优设">图酷优设</a>',
'wordpress中文主题' => '<a href="https://www.xtuku.com" rel="bookmark" title="wordpress中文主题">wordpress中文主题</a>',
'关键词' => '<a href="http://wanlimm.com/" rel="external nofollow" rel="bookmark" title="说明">关键词</a>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter('the_content', 'content_keywords_link');
文章标题:网站SEO优化WordPress给文章关键词标签自动添加内链
文章链接:https://www.xtuku.com/11425.html
更新时间:2022年09月27日
声明:本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:590173@qq.com我们将第一时间处理! 资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。 所有资源仅限于参考和学习,版权归原作者所有,更多请阅读新图酷网网络免责服务协议。