[教程] typecho常用文章数据调用方法,汇总整理

查看: 853|回复: 0
crll 发表于 2023-4-6 15:33:03
此文章内包括typecho热门文章调用,热评文章调用,随机调用,字数排行,根据指定id调用,调用相关文章,根据分类调用,以及根据标签调用的方法,看起来是不是特别的全,基本汇总了网上所有的方法,不过值得一提的是,我提供的方法自由度更高,而不是直接像常用的那样,把html的标签代码都写在了function.php里面。好吧,我承认是之前写过一篇这样的文章,typecho二级开发实例,新增Widget接口,然后看到qq爹博客大佬里一篇这样的文章,Typecho自定义调用如热门文章随机文章等,顿时觉得惊为天人,原来wdget也是可以直接在function,php里面定义的。

于是这篇文章就诞生了,做个汇总也好,以后开发模板直接来复制。
部分内容直接复制的泽泽博客,望大佬海量。

调用热评文章:
  1. class Widget_Contents_Post_Comments extends Widget_Abstract_Contents
  2. {
  3.     public function execute()
  4.     {
  5.         $this->parameter->setDefault(array('pageSize' => $this->options->postsListSize));
  6.         $this->db->fetchAll($this->select()
  7.         ->where('table.contents.status = ?', 'publish')
  8.         ->where('table.contents.created < ?', $this->options->time)
  9.         ->where('table.contents.type = ?', 'post')
  10.         ->order('commentsNum', Typecho_Db::SORT_DESC)
  11.         ->limit($this->parameter->pageSize), array($this, 'push'));
  12.     }
  13. }
复制代码


前台模板写法:
  1. <?php $this->widget('Widget_Contents_Post_Comments','pageSize=10')->to($Comments);while($Comments->next()): ?>
  2. 循环内代码
  3. <?php endwhile; ?>
复制代码


调用热门文章(注意先实现阅读量功能):
  1. class Widget_Post_hot extends Widget_Abstract_Contents
  2. {
  3.     public function __construct($request, $response, $params = NULL)
  4.     {
  5.         parent::__construct($request, $response, $params);
  6.         $this->parameter->setDefault(array('pageSize' => $this->options->commentsListSize, 'parentId' => 0, 'ignoreAuthor' => false));
  7.     }
  8.     public function execute()
  9.     {
  10.         $select  = $this->select()->from('table.contents')
  11. ->where("table.contents.password IS NULL OR table.contents.password = ''")
  12. ->where('table.contents.status = ?','publish')
  13. ->where('table.contents.created <= ?', time())
  14. ->where('table.contents.type = ?', 'post')
  15. ->limit($this->parameter->pageSize)
  16. ->order('table.contents.views', Typecho_Db::SORT_DESC);
  17. $this->db->fetchAll($select, array($this, 'push'));
  18.     }
  19. }
复制代码

前台模板写法:
  1. <?php $this->widget('Widget_Post_hot@hot', 'pageSize=6')->to($hot);while($hot->next()): ?>
  2. 循环内代码
  3. <?php endwhile; ?>
复制代码


调用随机文章:
  1. class Widget_Contents_Post_Rand extends Widget_Abstract_Contents
  2. {
  3.     public function execute()
  4.     {
  5.     $this->parameter->setDefault(array('pageSize' => $this->options->postsListSize));
  6.         $this->db->fetchAll($this->select()
  7.         ->where('table.contents.status = ?', 'publish')
  8.         ->where('table.contents.created < ?', $this->options->time)
  9.         ->where('table.contents.type = ?', 'post')
  10.         ->order('RAND()', Typecho_Db::SORT_DESC)
  11.         ->limit($this->parameter->pageSize), array($this, 'push'));
  12.     }
  13. }
复制代码

前台模板写法:
  1. <?php $this->widget('Widget_Contents_Post_Rand','pageSize=10')->to($Rand);while($Rand->next()): ?>
  2. 循环内代码
  3. <?php endwhile; ?>
复制代码


根据字数调用:
  1. class Widget_Contents_Post_Size extends Widget_Abstract_Contents
  2. {
  3.     public function execute()
  4.     {
  5.         $this->parameter->setDefault(array('pageSize' => $this->options->postsListSize));
  6.         $this->db->fetchAll($this->select()
  7.         ->where('table.contents.status = ?', 'publish')
  8.         ->where('table.contents.created < ?', $this->options->time)
  9.         ->where('table.contents.type = ?', 'post')
  10.         ->order('LENGTH(text)', Typecho_Db::SORT_DESC)
  11.         ->limit($this->parameter->pageSize), array($this, 'push'));
  12.     }
  13. }
复制代码

前台模板写法:
  1. <?php $this->widget('Widget_Contents_Post_Size','pageSize=10')->to($Size);while($Size->next()): ?>
  2. 循环内代码
  3. <?php endwhile; ?>
复制代码


调用指定文章:
  1. class Widget_Post_fanjubiao extends Widget_Abstract_Contents
  2. {
  3.     public function __construct($request, $response, $params = NULL)
  4.     {
  5.         parent::__construct($request, $response, $params);
  6.         $this->parameter->setDefault(array('pageSize' => $this->options->commentsListSize, 'parentId' => 0, 'ignoreAuthor' => false));
  7.     }
  8.     public function execute()
  9.     {
  10.         $select  = $this->select()->from('table.contents')
  11. ->where("table.contents.password IS NULL OR table.contents.password = ''")
  12. ->where('table.contents.type = ?', 'post')
  13. ->limit($this->parameter->pageSize)
  14. ->order('table.contents.modified', Typecho_Db::SORT_DESC);

  15. if ($this->parameter->fanjubiao) {
  16. $fanju=explode(",",$this->parameter->fanjubiao);
  17. $select->where('table.contents.cid in ?', $fanju);
  18. }
  19. $this->db->fetchAll($select, array($this, 'push'));
  20.     }
  21. }
复制代码

前台模板写法:
  1. <?php $week1="728,1197";
  2. $this->widget('Widget_Post_fanjubiao@fanjubiao', 'fanjubiao='.$week1)->to($fanju);while($fanju->next()): ?>
  3. 循环内代码
  4. <?php endwhile; ?>
复制代码


调用相关文章(文章详情页面使用)
  1. <?php $this->related(6)->to($relatedPosts); ?>
  2. 循环内代码
  3. <?php endwhile; ?>
复制代码


根据分类调用文章:
  1. <?php $this->widget('Widget_Archive@index1', 'pageSize=5&type=category', 'mid=分类ID')->to($categoryPosts);while($categoryPosts->next()):?>
  2. 循环内代码
  3. <?php endwhile;?>
复制代码


根据标签调用:
  1. <?php $this->widget('Widget_Archive@indexessence', 'pageSize=2&type=tag', 'slug=精华')->to($indexessence);while($indexessence->next()): ?>
  2. 循环内代码
  3. <?php endwhile; ?>
复制代码

以上就是整理出来的内容,如果以后折腾出新的,还会继续进行补充。
博主论坛 bzlt.net
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于本站联系我们FAQ友情链接免责声明生存法则

Build with for "make" Copyright © 2020-2022. Powered by Discuz! GMT+8, 2024-4-16 15:02

快速回复 返回顶部 返回列表