这会这个调用厉害了,我能想到的参数都放上了
这会这个调用厉害了,我能想到的参数都放上了,还能差什么参数?有这一个玩意配上自定义字段就天下无敌了。
$this->widget('Random@anything', 'order=viewsNum&limit=6&date=Week&asc=1&authorid=1&mids=1,2,3,4&fields=thumbnail')->to($anything);
class Random extends Widget_Abstract_Contents
{
/**
* 增加条件调用
*
* 方法:$this->widget('Random@anything', 'order=viewsNum&limit=6&date=Week&asc=1&authorid=1&mids=1,2,3,4&fields=thumbnail')->to($anything);
* order参数:viewsNum(*),commentsNum,likesNum(*),rand(),cid,slug,默认created,*的你不一定有这个。
* asc参数: 参数任意,有就行,用于倒序排列
* date参数:Day,Week,Month,Year,All 数字以秒计算往前推,All是全部,也是默认选项
* mids参数:分类的mid,英文逗号隔开所有分类
* limit参数:就是限制调用个数
* authorid参数:指定作者id,默认不指定
* fields参数:自定义字段的参数,就是自定义,可以用于各种指定调用,我主要用来调用置顶、幻灯片。
*/
public function execute()
{
//设置默认参数,就是调用时候一个参数不设置的时候,只有这三个参数
$this->parameter->setDefault(['limit' => $this->options->pageSize, 'order' => 'created','date' => 'All']);
//设置时间期限data参数
switch ($this->parameter->date) {
case 'Day' : $this->parameter->date = 86400; break;
case 'Week' : $this->parameter->date = 604800; break;
case 'Month' : $this->parameter->date = 2592000; break;
case 'Year' : $this->parameter->date = 31536000; break;
case 'All' : $this->parameter->date = $this->options->time; break;
}
//设置排序参数order
$order = $this->parameter->order;
if ($order=rand()){
$order = $this->parameter->order;
} else {
$order = 'table.contents.'.$this->parameter->order;
}
//通用选择,包含了日期和排序
$select= $this->select()->from('table.contents')
->join('table.relationships', 'table.contents.cid = table.relationships.cid')
->where('table.contents.type = ?', 'post')
->where('table.contents.status = ?', 'publish')
->where('table.contents.created < ?', $this->options->time)
->where('table.contents.created > ?', $this->options->time - $this->parameter->date)
->where('table.contents.type = ?', 'post')
->order($order, $this->parameter->asc?Typecho\Db::SORT_ASC:Typecho\Db::SORT_DESC);
//指定作者id判断
if($this->parameter->authorid){
$select = $select->where('table.contents.authorid = ?', $this->parameter->authorid);
}
//判断是否指定分类,标签也行
if($this->parameter->mids){
$mids = explode(',', $this->parameter->mids);
$mids = array_unique($mids);
$select = $select->where('table.relationships.mid in ?', $mids);
}
//自定义字段的判断
if($this->parameter->fields) {
$select = $select
->join('table.fields', 'table.contents.cid = table.fields.cid')
->where('table.fields.name = ?', $this->parameter->fields);
}
$select = $select
->group('table.contents.cid')
->limit($this->parameter->limit);
$this->db->fetchAll($select,[$this, 'push']);
}
}
最后修订于 2023-12-16 12:20:20