wordpress知识库
网站首页 > 知识库 > wordpress知识 >

WordPress给文章自动添加内链

2012/07/06

内链是一种优化网站结构和提升用户体验的策略,它在WordPress中可以通过自动添加内链来实现。内链是指在文章内容中将关键词或短语链接到网站内部的其他相关页面。这种链接可以帮助读者更深入地了解相关主题,并提供额外的价值和信息。通过内链,不仅可以增加网站页面之间的互联性,还可以引导用户在网站中导航和探索更多内容。

内链的好处是多方面的。首先,它可以改善SEO(搜索引擎优化),因为搜索引擎会将内链视为网站内部链接的信号,有助于提高页面的权重和排名。其次,内链可以提高用户留存率和页面浏览量,因为读者可以通过内链在相关主题上持续阅读,并深入探索网站。此外,内链还可以帮助读者快速定位相关信息,提供更完整的知识体系。

自动添加内链的功能可以帮助网站管理员在撰写文章时省去手动添加内链的麻烦。通过在文章中识别关键词或短语,并自动将其链接到相关页面,自动内链插件可以节省时间和精力,同时确保文章中的内链合理布局和一致性。这样,网站管理员可以更专注于内容创作,提供更丰富和有价值的文章给读者。

使用WordPress内链插件,您可以轻松实现自动添加内链的功能,并提升网站的SEO效果、用户体验和内容可访问性,直接将如下代码添加到主题函数文件中即可。

//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, '$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);

相关阅读:

WordPress指定关键词自动加链接