Typecho中调用最新文章

获取最新文章,使用的Widget是Widget_Contents_Post_Recent

$obj = $this->widget('Widget_Contents_Post_Recent');
if($obj->have()){
    while($obj->next()){
        $obj->title();
        ... ...
    }
}else{
    echo '无最新文章';
}

以上例子,$obj的可用字段,可参考《Typecho模板中的Archive.php》一文。

可用参数
  • pageSize 获取最新文章的数目。
    $this->widget('Widget_Contents_Post_Recent','pageSize=10')则获取最新的10篇文章
常见用法

常规用法

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

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

快捷用法

通过parse方法,简化上述例子中的代码

$this->widget('Widget_Contents_Post_Recent')->parse('<a href="{permalink}">{title}</a>');

至于有哪些字段可用,可参阅《Typecho模板中的Archive.php》一文