联系官方销售客服

1835022288

028-61286886

分享经验 版主:论坛审计组
如何实现文章内链(关键词自定义URL版)
类型:迅睿CMS 更新时间:2023-12-30 15:57:32 内链


如何实现给文章内链自定义关键词(关键词自定义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楼    小黄人 18html
    2022-06-24 14:47:02
    Chrome 0
    哈哈,多余了,官方版的也是可以自定义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楼    小黄人 18html
    2022-06-24 15:03:16
    Chrome 0
    {dr_content_link($neilian,$content,1)}
  • 李齐恩
    #3楼    李齐恩
    2022-06-24 15:07:30
    Chrome 0
    👍
  • 小黄人 18html
    #4楼    小黄人 18html
    2022-06-24 15:53:32
    Chrome 0
    看一楼和二楼就可以了,打赏部分的代码不需要看了,没意义!因为官方的标签可以实现,所以不需要去看它!
  • qijifuwu
    #5楼    qijifuwu
    2022-11-13 00:26:32
    小米手机 0
    学习了 感谢分享
  • wangxuanyou
    #6楼    wangxuanyou
    2023-12-30 15:57:32
    Chrome 0
    放在 config/custom.php 的格式是怎么样的?