typecho常用代码片段收集

typecho 获取最新文章,指定数量

<?php $this->widget('Widget_Contents_Post_Recent','pageSize=5')->to($news);?>
  <?php if($news->have()):?>
    <?php while($news->next()): ?>

        <a href="<?php $news->permalink();?>">
        <h2 class="card-title"><?php $news->title(); ?></h2>
          </a>
    <?php endwhile; ?>
  <?php endif; ?>

和在使用博客的那个<?php while ($this->next()) : ?><?php endwhile; ?>代码一样的用法,$news和$this一样。

'pageSize=5'便是输出5篇最新文章,数字自己定义即可。

获取某个分类下的文章列表

$category = $this->widget('Widget_Archive@category', 'pageSize=6&type=category', 'mid=1');
while($category->next()){
    // todo here
    ... ...
}
mid表示分类id,type指定获取分类文章

获取某关键词的搜索结果

$search = $this->widget('Widget_Archive@search', 'pageSize=10&type=search', 'keywords=typecho');
while($search->next()){
    // todo here
    ... ...
}
type指定搜索类型,keywords指定搜索关键词

获取某个tag的文章列表

$tags = $this->widget('Widget_Archive@tag', 'pageSize=10&type=tag', 'mid=2');
//或者:$tags = $this->widget('Widget_Archive@tag', 'pageSize=10&type=tag', 'slug=tag_name');
while($tags->next()){
    // todo here
    ... ...
}
type指定tag类型,mid表示tag的id,slug表示tag的缩写名

获取某篇特定的文章

$post = $this->widget('Widget_Archive@post', 'type=post', 'cid=1');
$post->title();
... ...
type指定post进而获取文章,cid指定文章id
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/304
0 评论
2.9k

发表评论

!