Typecho自定义挪用
这是面向模板开发者的一篇干货文章,经由过程进修下面的两个事例,你能够经由过程调解数据库语句来完成自定义挪用文章,如随机文章等。
挪用热点文章
在functions.php中到场以下代码
class Widget_Post_hot extends Widget_Abstract_Contents{ public function __construct($request, $response, $params = NULL) { parent::__construct($request, $response, $params); $this->parameter->setDefault(array('pageSize' => $this->options->commentsListSize, 'parentId' => 0, 'ignoreAuthor' => false)); } public function execute() { $select = $this->select()->from('table.contents')->where("table.contents.password IS NULL OR table.contents.password = ''")->where('table.contents.status = ?','publish')->where('table.contents.created <= ?', time())->where('table.contents.type = ?', 'post')->limit($this->parameter->pageSize)->order('table.contents.views', Typecho_Db::SORT_DESC); $this->db->fetchAll($select, array($this, 'push')); }}然后在前台挪用文章时就能够如许写了
<?php $this->widget('[email protected]', 'pageSize=6')->to($hot); ?><?php while($hot->next()): ?>文章链接:<?php $hot->permalink() ?>文章标题:<?php $hot->title(); ?><!--等等--> <?php endwhile; ?>这类写法异常原生,使用方法也同typecho挪用某分类下的文章语法一致
挪用指定文章鸠合
在functions.php中到场以下代码
class Widget_Post_fanjubiao extends Widget_Abstract_Contents{ public function __construct($request, $response, $params = NULL) { parent::__construct($request, $response, $params); $this->parameter->setDefault(array('pageSize' => $this->options->commentsListSize, 'parentId' => 0, 'ignoreAuthor' => false)); } public function execute() { $select = $this->select()->from('table.contents')->where("table.contents.password IS NULL OR table.contents.password = ''")->where('table.contents.type = ?', 'post')->limit($this->parameter->pageSize)->order('table.contents.modified', Typecho_Db::SORT_DESC);if ($this->parameter->fanjubiao) {$fanju=explode(",",$this->parameter->fanjubiao);$select->where('table.contents.cid in ?', $fanju);} $this->db->fetchAll($select, array($this, 'push')); }}然后在前台挪用热点文章时就能够如许写了
<?php $week1="728,1197";//指定文章id鸠合多个文章中心用英文逗号离隔$this->widget('[email protected]', 'fanjubiao='.$week1)->to($fanju); ?><?php while($fanju->next()): ?>文章链接:<?php $fanju->permalink() ?>文章标题:<?php $fanju->title(); ?><!--等等--> <?php endwhile; ?>这类写法异常原生,使用方法也同typecho挪用某分类下的文章语法一致
总结
如许的写法只需晓得数据库语句,就能够定制种种本身所需的挪用文章!语法切近原生且内部支撑挪用种种函数,比方缩略图函数等等!

评论列表