自动为WordPress关键词添加链接(非插件)

  • 酉灿
  • WordPress
  • Jul 23, 2021

为wordpress日志关键词添加链接,增加内链和外链,有利于SEO,实现上述功能一般我们会通过安装插件来实现,比如:WP Keyword LinkSEO Smart Link等。

利用《批量替换WordPress文章中的文字》一文的方法,不用插件也可以实现自动为关键词添加内链和外链。

将下面代码粘贴到主题functions.php文件中:


 
  1. function replace_text_wps($text){
  2. $replace = array(
  3. 'HotNews' => '<a href="https://zmingcx.com/" rel="bookmark" title="HotNews Pro主题">HotNews</a>',
  4. '知更鸟' => '<a href="https://zmingcx.com/" rel="bookmark" title="知更鸟博客">知更鸟</a>',
  5. '关键词' => '<a href="https://zmingcx.com/" rel="bookmark" title="说明">关键词</a>'
  6. );
  7. $text = str_replace(array_keys($replace), $replace, $text);
  8. return $text;
  9. }
  10.  
  11. add_filter('the_content', 'replace_text_wps');
 
打赏