WordPress给文章关键词标签自动添加内链

『点击关注公众号』 关注=免费白嫖,取关=永久拉黑!

前言

今天给大家分享一个wordpress SEO优化小技巧,该技巧能够实现文章内的关键词标签自动添加内链
在写文章的时候,发布之前你会设置很多标签,比如模板啊,源码,等等,这些关键词,往往需要搜索才能找到,今天给大家分享这个是只要你的文章中包含了你网站的关键词,就会自动添加超链接,也是更好的协助网站收录优化,提高内页收录,内链的好坏,自然就不用说了,它可以让百度蜘蛛在你的网站爬行更多的地方。

当然,很多wordpress插件也可以实现这个功能,但是今天给大家分享的是不用插件,就可以给文章关键词标签自动添加内链的方法。

那怎么如何让wordpress站点的文章关键词标签自动添加内链呢?其实我们只需要在主题目录下的functions.php文件中添加一段代码就可以实现了。

效果图

图片[1]-WordPress给文章关键词标签自动添加内链-悠悠小木屋-文案馆_表情包_教程资源分享

代码

//by 悠悠小木屋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 = 1;  //一个标签最多链接几次
  $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, '%&&&&&%', $content);
      $content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '%&&&&&%', $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);

在主题目录下的functions.php文件中添加上面代码就可以了,主题更新记得备份哦。

泼天的富贵轮到你啦: 大流量卡,官方可查,长期套餐!免费加盟我们,推广还可赚取高额佣金!点击查看详情~
THE END
点赞14 分享
神评区域 抢沙发

请登录后发表评论

    暂无评论内容