我们在购物的时候是不是希望购买别人也在购买的商品?我们在访问别人网站的时候是不是也想看看网站中有哪些文章也是大家关注的,于是我们会在制作主题模板的时候调用最热门的文章,比如调用评论数最多的,或者是访问量最高的文章。在这篇文章中,老蒋准备整理来自Typecho主题中调用评论最多的文章。

// 评论最多的文章 重新整理来自itbulu.com
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>';        
    }
}

}


我们可以将代码添加到当前Typecho主题的Functions.php文件中。
<?php getHotComments('10');?>

然后在需要的位置调用。数量可以自己修改。


扫描二维码,在手机上阅读!