本文代码摘自PHPCMS官方论坛会员“世界首富”的帖子,特此致谢!
第一步:修改phpcmsmodulescontentclassescontent_tag.class.php,在最后面的 } 前面添加如下代码:
public function newcontent($data){ $num = intval($data['limit']) ? intval($data['limit']) : '20'; // 设置排序 Switch($data['order']){ case '1': $order = ' `id` ASC '; break; case '2': $order = ' `id` DESC '; break; case '3': $order = ' `inputtime` ASC '; break; case '4': $order = ' `inputtime` DESC '; break; case '5': $order = ' `updatetime` ASC '; break; case '6': $order = ' `updatetime` DESC '; break; default: $order = ' `id` DESC '; } if($data['catid']){ $catids = explode(',', $data['catid']); foreach($catids as $catid){ $catid = intval($catid); if(empty($catid))continue; $this->set_modelid($catid); $where = $this->category[$catid]['child'] ? ' `catid` IN ('.$this->category[$catid]['arrchildid'].')' : " `catid` = $catid"; $datas = $this->db->select($where, '*', $num, $order); $data[$catid]['data'] = $datas; // 记录本次的文章数 $data['num'][] = count($datas); $model_num++; } }else{ $models = getcache('model', 'commons'); foreach($models as $model){ $this->db->set_model($model['modelid']); $datas = $this->db->select('', '*', $num, $order); $data[$model['modelid']]['data'] = $datas; // 记录本次的文章数 $data['num'][] = count($datas); $model_num++; } } if($data){ // 获取每个模型应该截取的条数 $num = ceil($num/$model_num); // 循环条数记录用于找出条数不满足的数量然后进行平均 $w_num = $w_num_t = ''; foreach($data['num'] as $num_t){ if($num_t < $num){ $w_num += $num-$num_t; $w_num_t++; } } // 判断是否有不满足平均数的 如果有那么就增加平均值 if($w_num_t){ $num += ceil($w_num/($model_num-$w_num_t)); } $datas = array(); foreach($data as $r){ $r_n = ''; if(is_array($r['data'])) foreach($r['data'] as $r_t){ $datas[] = $r_t; if(++$r_n == $num)break; } } return $datas; }else{ return false; } }
第二部:模板调用,与PHPCMS自带的标签调用方法差不多,为了方便,我直接把本站用到的代码分享给大家:
{pc:content action="newcontent" num="100" order="id DESC" cache="3600"} {loop $data $r} <a href="{$r[url]}" title="{$r[title]}" target="_blank">{$r[title]}</a></dt> {/loop} {/pc}
参数说明:
catid-可有可无,加上则只调用指定栏目的最新内容,如调用多个栏目请使用英文半角的“,”间隔(不包括引号); order-排序,参数值1-7,具体含义可参考新增代码内容,里面写的很具体了; num-调用数量,不指定则默认显示20条; 最后强调一下,使用utf-8代码的朋友请不要用win下的记事本编辑内容,以免乱码!