我们知道Typecho博客是自带调用相关博客的功能的,但是如果我们想要调用最新博客,或者指定数量的博客文章,Typecho自身功能就不能够满足了,还好作为开源博客,Typecho是支持我们修改添加代码来实现调用Typecho指定数量最新文章的功能的,有三个比较简单的代码实现法,我们简单分享给大家:

方案一

<?php
$this->widget('Widget_Contents_Post_Recent','pageSize=10')->parse('<li><a href="https://www.yuucn.com/{permalink}"><span>{year}/{month}/{day}</span>{title}</a></li>');
?>

参数说明:
pageSize – 要调用的文章数量,默认调用后台“设置-阅读-文章列表数目”里设置的数量
{permalink} – 文章链接标签代码
{year} – 文章发布时间年份
{month} – 文章发布时间月份
{day} – 文章发布时间天
{title} – 文章标题

方案二

<?php
$this->widget('Widget_Contents_Post_Recent','pageSize=10')->to($recent);
if($recent->have()):
while($recent->next()):
?>
<li><a href="https://www.yuucn.com/<?php $recent->permalink();?>"><?php $recent->title();?></a></li>
<?php endwhile; endif;?>

方案三

<?php
$recent = $this->widget('Widget_Contents_Post_Recent','pageSize=10');
if($recent->have()):
while($recent->next()):
?>
<li><a href="https://www.yuucn.com/<?php $recent->permalink();?>"><?php $recent->title();?></a></li>
<?php endwhile; endif;?>

听过总结以上三个办法,我们可以发现三者的原理一直,都是使用Widget_Contents_Post_Recent这个函数中的widget实现调用,通过pageSize参数限制调用数量

提示:把代码pageSize=10中的10为要调用的文章数量。

函数位于var/Widget/Contents/Post/Recent.php

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