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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索