[常用代码]Typecho如何实现按时间轴显示文章列表

现在Typecho的时间轴主题中,基本上都是按照发布日期进行索引,如图。
![时间轴主题截图][1]

而如果,我想制作类似的时间轴页面,但又需要按照时间(年份或者月份)进行归档,需要怎么弄呢?
![按时间归档兼时间轴展示][2]

做起来其实很简单,这里附上TypechoDev的实现代码,有需要的客官直接剪走吧!

parameter->setDefault(‘format=Y-m&type=month&limit=0’);

$select = $this->db->select(‘table.contents.created,table.contents.cid,table.contents.title’)->from(‘table.contents’)
->where(‘type = ?’, ‘post’)
->where(‘table.contents.status = ?’, ‘publish’)
->where(‘table.contents.created < ?', $this->options->gmtTime)
->order(‘table.contents.created’, Typecho_Db::SORT_DESC);

$resource = $this->db->query($select);

$offset = $this->options->timezone – $this->options->serverTimezone;
$result = array();
while ($post = $this->db->fetchRow($resource)) {
$post[‘permalink’]=Typecho_Router::url(‘post’,$post, $this->options->index);
$timeStamp = $post[‘created’] + $offset;
$date = date($this->parameter->format, $timeStamp);

if (isset($result[$date])) {
$result[$date][‘count’] ++;
$result[$date][‘list’][]=$post;
} else {
$result[$date][‘year’] = date(‘Y’, $timeStamp);
$result[$date][‘month’] = date(‘m’, $timeStamp);
$result[$date][‘day’] = date(‘d’, $timeStamp);
$result[$date][‘date’] = $date;
$result[$date][‘count’] = 1;
$result[$date][‘list’]=array($post);
}
}

if ($this->parameter->limit > 0) {
$result = array_slice($result, 0, $this->parameter->limit);
}

foreach ($result as $row) {
$row[‘permalink’] = Typecho_Router::url(‘archive_’ . $this->parameter->type, $row, $this->widget(‘Widget_Options’)->index);
$this->push($row);
}
}

}

[1]: https://www.typechodev.com/usr/uploads/2015/03/4141853618.png
[2]: https://www.typechodev.com/usr/uploads/2015/03/1757775573.png

原创文章,作者:admin,如若转载,请注明出处:https://www.typechodev.com/theme/476.html

(0)
上一篇 2015-02-16 上午6:38
下一篇 2015-02-28 上午4:55

相关推荐

发表评论

您的电子邮箱地址不会被公开。