给wordpress文章内容中标签文本自动添加链接的代码

目前有很多站长不为 wordpress文章添加Tag标签,可能原因就是每次发布文章都要手动去找为文章添加一堆Tag标签,有时还不知文章是否出现以前用过的标签,总之有点太麻烦了。那就更不会给wordpress文章内容中标签文本自动添加链接做内联了还是嫌麻烦。

那之前的 为wordpress文章自动添加已有标签tag的纯代码方法 之后,本文就是让 WordPress文章自动添加的Tag标签在内容正文本中出现直接变成内链的方法,以达到你的WordPress站点实现自动为文章添加Tag标签,并自动为这些标签添加链接变成内连接的 WordPress标签链接全自动添加方案。

WordPress标签添加内链接

方法很简单,将下面的代码添加到当前主题的 functions.php 文件中即可:

  1. /**
  2. * WordPress 自动为文章标签添加该标签的链接
  3. */
  4. function wpkj_auto_add_tag_link($content){
  5. $limit = 1; // 设置同一个标签添加几次链接
  6. $posttags = get_the_tags();
  7. if ($posttags) {
  8. foreach($posttags as $tag) {
  9. $link = get_tag_link($tag->term_id);
  10. $keyword = $tag->name;
  11. $cleankeyword = stripslashes($keyword);
  12. $url = '<a target="_blank" href="'.$link.'" title="'.str_replace('%s', addcslashes($cleankeyword, '$'), __('View all posts in %s')).'">'.addcslashes($cleankeyword, '$').'</a>';
  13. $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s';
  14. $content = preg_replace($regEx,$url,$content,$limit);
  15. }
  16. }
  17. return $content;
  18. }
  19. add_filter( 'the_content', 'wpkj_auto_add_tag_link', 1 );

其中,第6行参数和第16行“View all posts in”可以自行调整;还有另一种同样功能的代码:

  1. /* 自动为文章内的标签添加内链 */
  2. $match_num_from = 1; //一篇文章中同一个标签少于几次不自动链接
  3. $match_num_to = 1; //一篇文章中同一个标签最多自动链接几次
  4. function tag_sort($a, $b){
  5. if ( $a->name == $b->name ) return 0;
  6. return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
  7. }
  8. function tag_link($content){
  9. global $match_num_from,$match_num_to;
  10. $posttags = get_the_tags();
  11. if ($posttags) {
  12. usort($posttags, "tag_sort");
  13. foreach($posttags as $tag) {
  14. $link = get_tag_link($tag->term_id);
  15. $keyword = $tag->name;
  16. $cleankeyword = stripslashes($keyword);
  17. $url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('【查看含有[%s]标签的文章】'))."\"";
  18. $url .= ' target="_blank"';
  19. $url .= ">".addcslashes($cleankeyword, '$')."</a>";
  20. $limit = rand($match_num_from,$match_num_to);
  21. $content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
  22. $content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
  23. $cleankeyword = preg_quote($cleankeyword,'\'');
  24. $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;
  25. $content = preg_replace($regEx,$url,$content,$limit);
  26. $content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
  27. }
  28. }
  29. return $content;
  30. }
  31. add_filter('the_content','tag_link',1);

以上代码的功能就是在我们发布/保存/更新文章时,自动检测文章中的内容,是否出现标签内容。如果出现过就会自动为文章内的标签添加内链。如这篇文章有标签:荐爱小站 那么只要我们的文章内容中出现有荐爱小站的,那么就会自动为“荐爱小站”添加标签链接变成内链。(实测第一种代码在 WordPress 5.4.4 版本中有效)

当我们wordpress网站的Tag标签已经非常多的情况下,使用这两篇文章的代码就能实现 WordPress站点自动为文章添加标签和标签内链的效果,那么将会大大减少我们的编辑工作量。如果大家平时不喜欢人工添加标签的,不妨试试这两个代码。

 

转载请注明链接地址:荐爱小站 » 给wordpress文章内容中标签文本自动添加链接的代码

赞 (2) 赏 !

觉得文章有用就打赏一下吧,赠人玫瑰手有余香!

支付宝扫一扫打赏

微信扫一扫打赏