如何实现文章内链(关键词自定义URL版)

迅睿框架 更新 2024-08-24 14:19:22 小黄人 18html


如何实现给文章内链自定义关键词(关键词自定义URL版)

官方内链 是直接输出 搜索地址或者是  tag库地址

https://www.xunruicms.com/doc/405.html



但是很多人希望是自定义URL,那就参考下面去写


将下面代码放在   config/custom.php  里

调用方法:{tag_link($content,$neilian)}


这里的 $kws 是一个数组

格式为:  ['关键词'=>URL,'关键词'=>URL,'关键词'=>URL]

我们可以通过全局表单实现这个关键词库,

新建一个表单,

内链库/nlk 

默认字段有  title

自定义一个字段  url


下面是 show.html 调用 ,首先用 form 循环输出一个关键词数组,然后填到 tag_link 函数里进行匹配

{form form=nlk num=10}
{php 
    $neilian = [];
    $neilian[$t.title] = $t.url;
}
{/form}

{tag_link($content,$neilian)}


//
// 将下面代码放在   config/custom.php  里
//
// 调用方法:
// {tag_link($content,$neilian)}
//
function tag_link($content,$posttags,$match_num_from = 1,$match_num_to = 1){

 //$match_num_from = 1; 一篇文章中同一个标签少于几次不自动链接
 //$match_num_to = 1; 一篇文章中同一个标签最多自动链接几次

 if ($posttags) {

  $new = [];
  foreach($posttags as $k => $v) {
   $new[] = ['link'=>$v,'name'=>$k];
  }
  $posttags = $new;

  usort($posttags, "tag_sort");
  foreach($posttags as $tag) {
   $link = $tag['link'];
   $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[^>]+>)(.*)('.$ex_word.')(.*)(</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;
}

function tag_sort($a, $b){
 if ( $a->name == $b->name ) return 0;
 return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}


方案

  • 小黄人 18html #1 · 2022-06-24 14:47:02
    哈哈,多余了,官方版的也是可以自定义URL的
    下面是 show.html 调用 ,首先用 form 循环输出一个关键词数组,然后填到 tag_link 函数里进行匹配
    
    {form form=nlk num=10}
    {php 
        $neilian = [];
        $neilian[$t.title] = $t.url;
    }
    {/form}
    
    {de_content_link($neilian,$content,1)}
  • 小黄人 18html #2 · 2022-06-24 15:03:16
    {dr_content_link($neilian,$content,1)}
  • 李齐恩 #3 · 2022-06-24 15:07:30
    👍
  • 小黄人 18html #4 · 2022-06-24 15:53:32
    看一楼和二楼就可以了,打赏部分的代码不需要看了,没意义!因为官方的标签可以实现,所以不需要去看它!
  • qijifuwu #5 · 2022-11-13 00:26:32
    学习了 感谢分享
  • wangxuanyou #6 · 2023-12-30 15:57:32
    放在 config/custom.php 的格式是怎么样的?
  • 天河 #7 · 2024-08-24 14:19:22
    学习了 感谢分享

迅睿系统漏洞

GeoSeo收录宝
GeoSeo收录宝(2026-09-01)
升级 ai智能发布功能 升级 支持8国语言后端设置和前端输出功能 ...
Word发布批量上传
Word发布批量上传(2026-08-31)
版本覆盖问题重发
多语言翻译Seolang
多语言翻译Seolang(2026-08-31)
新增 LLM 大模型对话翻译模块 配置层强类型化重构 URL 模...
自定义存储
自定义存储(2026-08-30)
同步服务器端通用版实现,统一管理端、CMS 端和浏览器直传的提示与错...
GeoSeo工具箱
GeoSeo工具箱(2026-08-29)
新增 bing推送 新增 IndexNow推送 新增 baidu...
外贸多语言建站
外贸多语言建站(2026-08-27)
新增阿里云专业版入口 新增阿里云自动识别字符格式
Querylist渐进式采集
Querylist渐进式采集(2026-08-26)
增加范围截取规则
实时任务
实时任务(2026-08-26)
补充并整理完整使用说明、历史版本更新日志,统一帮助页样式。