再来个抛砖引玉 内容页根据关键词调用相关内容 新功能哦!

2020-03-10 2376 次

昨天升级了一个隐藏的功能,今天就简单的做个说明怎么用,反正也不能浪费不是 那就用内容页面关键词读取相关内容为例吧。

前台是你模型中已经存在 keywords 字段  关键词支持 空格分割,号分割 那么就开始新功能之旅吧。

我们在 application 目录中新建一个 api目录 api目录再新建一个 index 目录 也就是 application\api\index  下面新建一个Keywords.php

 <?php
namespace app\api\index;
/**
 * 动态获取内容关键词并读取相关内容
 * Class Keywords
 * @package app\api\index
 */
class Keywords{
	public function index($text){
		$cfg=unserialize($text['cfg']);
        $biao=db($cfg['systype'].'_content')->where('id',$cfg['id'])->find(); // 查找这个内容的所在模型
		$tags=db($cfg['systype'].'_content'.$biao['mid'])->where('id',$cfg['id'])->find(); //查找到模型后去模型表读取这个内容的信息
		$detail=strpos($tags['keywords'],',')!==false?explode(',',$tags['keywords']):explode(' ',$tags['keywords']); //根据这个信息的 keywords 字段进行拆分检索 keywords 可以是自己定义的任意字段
		$where=[];
		$where_tit=[];
		array_push($where_tit,'like');
		$ARR=[];
		for($i=0;$i<count($detail);$i++){
			array_push($ARR,'%'.$detail[$i].'%');
		}
		array_push($where_tit,$ARR);
		array_push($where_tit,'OR');
		$where['title']=$where_tit;
		$array=db($cfg['systype'].'_content'.$biao['mid'])->where($where)->order($cfg['order'],$cfg['by'])->limit($cfg['rows'])->select();
		foreach($array AS $k=>$rs){
			$data[$k]['title']=$rs['title'];
			$data[$k]['create_time']=$rs['create_time'];
			$data[$k]['picurl']=tempdir($rs['picurl']);
			$rs['content']=preg_replace('/<([^<]*)>/is',"",$rs['content']);
			$data[$k]['content']=get_word($rs['content'],500);
			$data[$k]['url']=url($cfg['systype'].'/content/show',['id'=>$rs['id']],'html',true);
		}
		return $data;
	}
}

临时拼凑的代码bug难免 仅仅是示例。

怎么使用呢?

template\index_style\default\cms\content\pc_show.htm 强烈建议自己复制default一份再改不然升级会覆盖


在你需要的地方加上


 {qb:tag name="pc_show" rows="9" type="cms" union='id' class='app\api\index\Keywords@index'}

<div class="lists"><a href="{$rs.url}">{$rs.title}</a></div>


{$rs.url} 内容网址

{$rs.title} 标题

{$rs.create_time} 发布时间

{$rs.picurl}  图片

{$rs.content}  内容

{/qb:tag}

rows 调用数量

type 调用的模块 你shop模块就写 =shop

order 排序 


是不是很好用 原理是union动态读取内容的id根据内容id查找所在模型和内容并把keywords拆分 用他作为关键词去查找内容。


感谢齐博的无私奉献,官方一直在努力 

ps:2020年1月15日已经修正


标签
声明:
1.本站的资源收集于互联网,仅供大家学习交流,请勿用于非法用途,否则后果自负!
2.如转载本站的原创文章,请勿必注明文章来源,对于不尊重原创的行为我们将追究责任