typecho常用文章数据调用方法,汇总整理

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

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

调用热评文章:

class Widget_Contents_Post_Comments extends Widget_Abstract_Contents
{
    public function execute()
    {
        $this->parameter->setDefault(array('pageSize' => $this->options->postsListSize));
        $this->db->fetchAll($this->select()
        ->where('table.contents.status = ?', 'publish')
        ->where('table.contents.created < ?', $this->options->time)
        ->where('table.contents.type = ?', 'post')
        ->order('commentsNum', Typecho_Db::SORT_DESC)
        ->limit($this->parameter->pageSize), array($this, 'push'));
    }
}

前台模板写法:

<?php $this->widget('Widget_Contents_Post_Comments','pageSize=10')->to($Comments);while($Comments->next()): ?>
循环内代码
<?php endwhile; ?>

调用热门文章(注意先实现阅读量功能):

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);while($hot->next()): ?>
循环内代码
<?php endwhile; ?>

调用随机文章:

class Widget_Contents_Post_Rand extends Widget_Abstract_Contents
{
    public function execute()
    {
    $this->parameter->setDefault(array('pageSize' => $this->options->postsListSize));
        $this->db->fetchAll($this->select()
        ->where('table.contents.status = ?', 'publish')
        ->where('table.contents.created < ?', $this->options->time)
        ->where('table.contents.type = ?', 'post')
        ->order('RAND()', Typecho_Db::SORT_DESC)
        ->limit($this->parameter->pageSize), array($this, 'push'));
    }
}

前台模板写法:

<?php $this->widget('Widget_Contents_Post_Rand','pageSize=10')->to($Rand);while($Rand->next()): ?>
循环内代码
<?php endwhile; ?>

根据字数调用:

class Widget_Contents_Post_Size extends Widget_Abstract_Contents
{
    public function execute()
    {
        $this->parameter->setDefault(array('pageSize' => $this->options->postsListSize));
        $this->db->fetchAll($this->select()
        ->where('table.contents.status = ?', 'publish')
        ->where('table.contents.created < ?', $this->options->time)
        ->where('table.contents.type = ?', 'post')
        ->order('LENGTH(text)', Typecho_Db::SORT_DESC)
        ->limit($this->parameter->pageSize), array($this, 'push'));
    }
}

前台模板写法:

<?php $this->widget('Widget_Contents_Post_Size','pageSize=10')->to($Size);while($Size->next()): ?>
循环内代码
<?php endwhile; ?>

调用指定文章:

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";
$this->widget('Widget_Post_fanjubiao@fanjubiao', 'fanjubiao='.$week1)->to($fanju);while($fanju->next()): ?>
循环内代码
<?php endwhile; ?>

调用相关文章(文章详情页面使用)

<?php $this->related(6)->to($relatedPosts); ?>
循环内代码
<?php endwhile; ?>

根据分类调用文章:

<?php $this->widget('Widget_Archive@index1', 'pageSize=5&type=category', 'mid=分类ID')->to($categoryPosts);while($categoryPosts->next()):?>
循环内代码
<?php endwhile;?>

根据标签调用:

<?php $this->widget('Widget_Archive@indexessence', 'pageSize=2&type=tag', 'slug=精华')->to($indexessence);while($indexessence->next()): ?> 
循环内代码
<?php endwhile; ?> 

以上就是整理出来的内容,如果以后折腾出新的,还会继续进行补充。

竹杖芒鞋轻胜马,也无风雨也无晴。

添加评论

发表评论 | 暂无评论

该文章已经关闭评论

  • 一起走过的日子 - 刘德华
  • 情书 - 张学友
  • 后会无期 - G.E.M.邓紫棋
  • 清白之年 (Live) - 朴树
  • My Love (Radio Edit) - Westlife