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('Widget_Post_hot@hot', '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('Widget_Post_fanjubiao@fanjubiao', 'fanjubiao='.$week1)->to($fanju); ?>
<?php while($fanju->next()): ?>
文章链接:<?php $fanju->permalink() ?>
文章标题:<?php $fanju->title(); ?>
<!--等等-->
<?php endwhile; ?>
这种写法非常原生,使用方法也同typecho调用某分类下的文章语法一致
总结
这样的写法只要懂得数据库语句,就可以定制各种自己所需的调用文章!语法贴近原生且内部支持调用各种函数,比如缩略图函数等等!
版权属于:泽泽社长
本文链接:https://blog.zezeshe.com/archives/typecho-custom-call.html
本站未注明转载的文章均为原创,并采用
CC BY-NC-SA 4.0 授权协议,转载请注明来源,谢谢!
如果要加个时间限定,如半年内最新门的文章,要怎么修改代码?
->where('table.contents.created > ?', '你的时间戳')
加个这个就可以限制文章创建时间需要大于你的时间戳了where('table.contents.created > ?','15552000') 没有效果 这个时间戳是什么呢?
时间戳 怎么写?PHP小白
time()
是当前的时间戳,比如你说半年,181x24x60x60=15638400,所以你的时间戳就是time()-15638400
好像没效果
自己研究吧,我没测试,只提供思路
@泽泽社长 调用指定分类的热门文章 代码还要怎么修改阿
不好意思才回复,需要join上relationships这个表然后根据mid来设置获取条件
我一直是这么干的,只不过没发出来
其实我本来也不舍得发出来
生活不止,轮子不停
奥里给~!
这个方法真的很好用,本来就想藏着自己用来着
为什么没有收到回复邮件通知。
我不来看看都不知道你回复我了~
看看这条消息能不能通知到你,我是能收到邮件提醒的
这条收到了