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

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

前台是你模型中已经存在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'}

{$rs.title}

  

{$rs.url} 内容网址

{$rs.title} 标题

{$rs.create\_time} 发布时间

{$rs.picurl}  图片

{$rs.content}  内容

{/qb:tag}

rows 调用数量

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

order 排序
复制

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

posted @ 2021-11-18 20:18  半抹灯芯  阅读(40)  评论(0)    收藏  举报