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

发表于 2020-02-27   |   分类于 typecho

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://qqdie.com/archives/typecho-custom-call.html

0

已有 7 条评论


  1. 亚马逊跟卖软件

    支持,不错的文章

    亚马逊跟卖软件  "2022-07-21 00:19:43" 发自Windows 10 回复
  2. 快乐编程,快乐生活

    腾不出时间来睡觉的人,迟早会腾出时间来生病;腾不出时间来恋爱的人,迟早会腾出时间来相亲

    快乐编程,快乐生活  "2022-07-14 17:08:41" 发自Windows 10 回复
  3. chenmo

    好的,谢谢。

    chenmo  "2020-05-22 10:53:50" 发自Android 回复
  4. chenmo

    分享在哪里?我也想加个这个功能!

    chenmo  "2020-05-22 10:40:50" 发自Android 回复
    1. 爱奇智

      我现在上班呢,有时间发给你

      爱奇智 admin "2020-05-22 10:51:16" 发自Android 回复
  5. chenmo

    博主,你这个评论点赞是怎么实现的?有代码分享吗?

    chenmo  "2020-05-21 22:21:12" 发自Windows 10 回复
    1. 爱奇智

      爱奇智 admin "2020-05-21 22:23:07" 发自Android 回复

发表新评论

© 2024 Powered by Typecho & Theme Quark