有EmlogPro文章自定义关键词内外链插件时发布插件,但是慢慢觉得对通过插件实现的功能最好还是直接集成在模板里那样才更高效、简洁。因为以前的标签内外链需要手动去添加操作,现在确实手动操作添加一些关键词太麻烦,于是用以下方法去替代:
实现步骤:
1、在需要添加的主题模板中找到module.php添加以下代码,可以根据具体情况添加多个重复的标签内链。
function tag_link($content){
global $CACHE;
$match_num_from = 1; //一个标签少于几次不链接
$match_num_to = 2; //一个标签最多链接几次
$tag_cache = $CACHE->readCache('tags');
foreach($tag_cache as $value){
$tag_url = Url::tag($value['tagurl']);
$keyword = $value['tagname'];
$cleankeyword = stripslashes($keyword);
$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>";
$regEx = ''(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))'s';
$tagbq= rand($match_num_from,$match_num_to);
$content = preg_replace($regEx,$url,$content,$tagbq);
}
return $content;
}
2、其次在echo_log.php中将文章内容输出挂载点 <?php echo $log_content; ?> 修改成 <?php echo tag_link($log_content); ?> 即可。
版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!