Typecho调用热门评论文章和调用最新文章的方法

Typecho程序在设计主题的时候,侧栏有些时候需要调用热评文章或者最新文章。我们可以在指定的位置通过脚本直接调用。在这篇文章中,老蒋准备整理这个调用文章的方法,以后在有需要的模板中可以直接调用使用。其实设计主题就那么回事,静态模板搞定后,就是直接调用。

第一、最新文章调用

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

调用最新10篇文章,可以根据需要修改数值。

第二、调用热评文章

function getHotComments($limit = 10){
$db = Typecho_Db::get();
$result = $db->fetchAll($db->select()->from('table.contents')
->where('status = ?','publish')
->where('type = ?', 'post')
->where('created <= unix_timestamp(now())', 'post')
->limit($limit)
->order('commentsNum', Typecho_Db::SORT_DESC)
);
if($result){
foreach($result as $val){
$val = Typecho_Widget::widget('Widget_Abstract_Contents')->push($val);
$post_title = htmlspecialchars($val['title']);
$permalink = $val['permalink'];
echo '<li><a href="'.$permalink.'" title="'.$post_title.'" target="_blank">'.$post_title.'</a></li>';
}
}
}

在当前主题Functions.php添加。

<?php getHotComments('10');?>

在需要的位置出现调用。

本文出处:老蒋部落 » Typecho调用热门评论文章和调用最新文章的方法 | 欢迎分享( 公众号:老蒋朋友圈 )

公众号 「老蒋朋友圈」获取站长新知 / 加QQ群 【1012423279】获取商家优惠推送