星空网络资源社区

标题: Emlog文章内容自动添加标签链接 [打印本页]

作者: 猪八戒    时间: 2025-4-14 07:05
标题: Emlog文章内容自动添加标签链接
早在EMLOG PRO-文章自定义关键词内外链插件时发布一个类似的插件,但是慢慢觉得的对通过插件实现的功能最好还是直接集成在模板里那样才更高效、简洁。因为以前的标签内外链需要手动去添加操作,现在确实手动操作添加一些关键词太麻烦,于是用以下方法去替代




1.在需要添加的主题模板中找到module.php添加以下代码,可以根据具体情况添加多个重复的标签内链


  1. function tag_link($content){
  2.         global $CACHE;
  3.         $match_num_from = 1;  //一个标签少于几次不链接
  4.         $match_num_to = 2;  //一个标签最多链接几次
  5.             $tag_cache = $CACHE->readCache('tags');
  6.             foreach($tag_cache as $value){
  7.                     $tag_url = Url::tag($value['tagurl']);
  8.                     $keyword = $value['tagname'];
  9.                     $cleankeyword = stripslashes($keyword);
  10.                     $url = "<a style=\"color: inherit;font-weight: inherit !important;text-decoration: none !important;border-bottom: 1px dashed rgb(14, 31, 218);\" href=\"{$tag_url}\" title=\"【查看含有[{$cleankeyword}]标签的文章】\" target=\"_blank\" >{$cleankeyword}</a>";
  11.                     $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s';
  12.                     $tagbq= rand($match_num_from,$match_num_to);
  13.                     $content = preg_replace($regEx,$url,$content,$tagbq);
  14.             }
  15.             return $content;
  16.     }
复制代码


2.其次在echo_log.php中将文章内容输出挂载点 <?php echo $log_content; ?> 修改成 <?php echo tag_link($log_content); ?> 即可






欢迎光临 星空网络资源社区 (http://bbs.hs57.cn/) Powered by Discuz! X3.2