有些时候,我们需要根据时间维度对文章进行归档,或者直接制作时间线(timeline)页,即用于展示博主的点滴。又有时,希望点击文章发布的时间即可直接连接到归档的页面,如发布时间2015年1月,点击“2015年1月”即可进入2015年1月发表的文章列表,这样可以在不占用任何额外的空间的前提下,方便读者按年,月,日阅览日志文章,增加流量。

Typecho提供了根据时间维度来获取文章归档的api,[Widget_Contents_Post_Date]

$obj = $this->widget('Widget_Contents_Post_Date');
if($obj->have()){
    while($obj->next()){
        $obj->date();
        ... ...
    }
}else{
    echo '无文章';
}

Widget_Contents_Post_Date字段解析

  • year:年份的字符串形式,譬如2015
  • month:月份的字符串形式,譬如01
  • day:字符串形式
  • date:字符串形式,具体格式由format参数指定,譬如参数指定为Y-m,则date字段输出2015-01
  • count:该时间归档下的文章数目
  • permalink:该时间归档的地址

譬如var_dump($obj)可能输出一下内容:

array (size=6)
  'year' => string '2015' (length=4)
  'month' => string '01' (length=2)
  'day' => string '25' (length=2)
  'date' => string '2015-01' (length=7)
  'count' => int 12
  'permalink' => string 'http://www.typechodev.com/index.php/2015/01/' (length=46)

Widget_Contents_Post_Date可用参数

  • format 设定输出格式,譬如参数指定为Y-m,则date字段输出2015-01。
  • type 设定归档类型,默认是month
  • limit 查询的条数,默认为0,则不限制,输出所有。

    `$this->widget('Widget_Contents_Post_Date','format=Y-m&type=month&limit=0')`,表示按月份为最小维度进行归档,展示的格式是Y-m,并显示所有归档。
    
Widget_Contents_Post_Date常见用法

常规用法

通过widget方法初始化组件,并使用next()进行迭代。

$this->widget('Widget_Contents_Post_Date')->to($recent);
while($recent->next()):
    ......
endwhile;

快捷用法

使用便捷方法parse:

$this->widget('Widget_Contents_Post_Date')->parse('<a href="{permalink}">{date}(count)</a>');