第一步:后台添加字段
添加字段:
后台-->内容-->内容相关设置-->模型管理-->文章模型-->字段管理-->添加字段-->如下图示例:
第二步:增加扩展函数:
将以下代码添加到 phpcms/libs/functions/extention.func.php 文件
懒人包下载地址 (注:makeurlrule函数对分页是否能传递相关参数很重要!)
第三步:前台模板调用
{php $sql = structure_filters_sql($modelid);}
{php $urlrule = makeurlrule()}
{pc:content action="lists" catid="$catid" where="$sql" modelid="$modelid" num="10" page="$page" moreinfo="1" urlrule="$urlrule" return="data" }
{loop $data $r}
........
{/loop}
{/pc}
一般网上都是到这一步就没有了,没有了......
但是你会发现,还有很多问题,下面就来解决
第一个问题:list条件下加入where后其他条件失效的问题
之前的一篇文章:解决lists标签中,加上where后其他条件失效的问题,其他条件失效了,如catid,thumb等等,也就是无法获取当前栏目的信息,而是把整个栏目下的所有文章都调用出来了!
打开/phpcms/modules/content/classes/目录下的content_tag.class.php这个文件,把下面的代码(大概第63行)
if(isset($data['where'])) {
$sql = $data['where'];
} else {
$thumb = intval($data['thumb']) ? " AND thumb != ''" : '';
if($this->category[$catid]['child']) {
$catids_str = $this->category[$catid]['arrchildid'];
$pos = strpos($catids_str,',')+1;
$catids_str = substr($catids_str, $pos);
$sql = "status=99 AND catid IN ($catids_str)".$thumb;
} else {
$sql = "status=99 AND catid='$catid'".$thumb;
}
}
替换为下面的代码即可。
if(isset($data['where'])) {
$where = (isset($data['where'])&&(!empty($data['where'])))?' AND '.$data['where']:'';
$thumb = intval($data['thumb']) ? " AND thumb != ''" : '';
if($this->category[$catid]['child']) {
$catids_str = $this->category[$catid]['arrchildid'];
$pos = strpos($catids_str,',')+1;
$catids_str = substr($catids_str, $pos);
$sql = "status=99".$where." AND catid IN ($catids_str)".$thumb;
} else {
$sql = "status=99".$where." AND catid='$catid'".$thumb;
}
} else {
$thumb = intval($data['thumb']) ? " AND thumb != ''" : '';
if($this->category[$catid]['child']) {
$catids_str = $this->category[$catid]['arrchildid'];
$pos = strpos($catids_str,',')+1;
$catids_str = substr($catids_str, $pos);
$sql = "status=99 AND catid IN ($catids_str)".$thumb;
} else {
$sql = "status=99 AND catid='$catid'".$thumb;
}
}
其实主要的修改思路是$where参数中并不包含当前栏目的id值,所以在$sql增加获取相关id的参数
第二个问题:分页数量不对
通过研究缓存得知,调用分页总数的函数是:
$content_total = $content_tag->count(array('catid'=>$catid,'where'=>$sql,'modelid'=>$modelid,'moreinfo'=>'1','limit'=>$offset.",".$pagesize,'action'=>'lists',));
通过这个,找到phpcms\modules\content\classes\content_tag.class.php大致36-56行,count函数如下:
public function count($data) {
if($data['action'] == 'lists') {
$catid = intval($data['catid']);
if(!$this->set_modelid($catid)) return false;
if(isset($data['where'])) {
$sql = $data['where'];
} else {
if($this->category[$catid]['child']) {
$catids_str = $this->category[$catid]['arrchildid'];
$pos = strpos($catids_str,',')+1;
$catids_str = substr($catids_str, $pos);
$sql = "status=99 AND catid IN ($catids_str)";
} else {
$sql = "status=99 AND catid='$catid'";
}
}
return $this->db->count($sql);
}
}
改为:
public function count($data) {
if($data['action'] == 'lists') {
$catid = intval($data['catid']);
$catids_str = $this->category[$catid]['arrchildid'];
if(!$this->set_modelid($catid)) return false;
if(isset($data['where'])) {
$sql = $data['where'];
if($this->category[$catid]['child']) {
$catids_str = $this->category[$catid]['arrchildid'];
$pos = strpos($catids_str,',')+1;
$catids_str = substr($catids_str, $pos);
$sql = $sql." AND catid IN ($catids_str)";
} else {
$sql = $sql." AND catid='$catid'";
}
} else {
if($this->category[$catid]['child']) {
$catids_str = $this->category[$catid]['arrchildid'];
$pos = strpos($catids_str,',')+1;
$catids_str = substr($catids_str, $pos);
$sql = "status=99 AND catid IN ($catids_str)";
} else {
$sql = "status=99 AND catid='$catid'";
}
}
return $this->db->count($sql);
}
}
你会发现,分页统计的修改原理仍然是$where条件下,没有传入当前栏目id这个参数!
大功告成,这样既然正常分页传参,也能正常获取当前栏目信息,获取的信息总数也正确了
但是,我并没有测试在get方式下,是不是有影响,欢迎大家测试!!!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!