typecho常用代码片段收集

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

  1. <?php $this->widget('Widget_Contents_Post_Recent','pageSize=5')->to($news);?>
  2. <?php if($news->have()):?>
  3. <?php while($news->next()): ?>
  4. <a href="<?php $news->permalink();?>">
  5. <h2 class="card-title"><?php $news->title(); ?></h2>
  6. </a>
  7. <?php endwhile; ?>
  8. <?php endif; ?>

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

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

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

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

获取某关键词的搜索结果

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

获取某个tag的文章列表

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

获取某篇特定的文章

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

发表评论

!