首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Typecho自定义调用如热门文章随机文章等

Typecho自定义调用如热门文章随机文章等

作者头像
泽泽社长
发布2023-04-17 15:40:16
发布2023-04-17 15:40:16
8200
举报
文章被收录于专栏:泽泽社泽泽社
Typecho自定义调用如热门文章随机文章等.jpg

Typecho自定义调用

这是面向模板开发者的一篇干货文章,通过学习下面的两个事例,你可以通过调整数据库语句来实现自定义调用文章,如随机文章等。

调用热门文章

functions.php中加入如下代码

代码语言:javascript
复制
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'));
    }
}

然后在前台调用文章时就可以这样写了

代码语言:javascript
复制
widget('Widget_Post_hot@hot', 'pageSize=6')->to($hot); ?>
next()): ?>
文章链接:permalink() ?>
文章标题:title(); ?>

这种写法非常原生,使用方法也同typecho调用某分类下的文章语法一致

调用指定文章集合

functions.php中加入如下代码

代码语言:javascript
复制
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'));
    }
}

然后在前台调用热门文章时就可以这样写了

代码语言:javascript
复制
widget('Widget_Post_fanjubiao@fanjubiao', 'fanjubiao='.$week1)->to($fanju); ?>
next()): ?>
文章链接:permalink() ?>
文章标题:title(); ?>

这种写法非常原生,使用方法也同typecho调用某分类下的文章语法一致

总结

这样的写法只要懂得数据库语句,就可以定制各种自己所需的调用文章!语法贴近原生且内部支持调用各种函数,比如缩略图函数等等!

linkCard('.post-content','0');

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Typecho自定义调用
  • 调用热门文章
  • 调用指定文章集合
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档