找回密码
 立即注册

将wordpress文章内容含标签的词转为链接

2022-7-23 21:44:02 · 站长社区
自动添加标签及内链的功能代码;只需要把代码复制到主题函数functions.php文件当中,功能就可以实现了
主题的functions.php文件添加如下代码:

  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);
  32. /* 自动为文章内的标签添加内链结束 */
复制代码




全部评论 0

自动添加标签及内链的功能代码;只需要把代码复制到主题函数functions.php文件当中,功能就可以实现了
主题的functions.php文件添加如下代码:

  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);
  32. /* 自动为文章内的标签添加内链结束 */
复制代码




热门推荐
您需要登录后才可以回帖 立即登录
说说你的想法......
0
0
0
返回顶部