typecho调用最热文章函数

首先在当前主题的文件夹中找到functions.php,打开在最末处加上以下代码

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 '

  • '.$post_title.'
  • ';
    }
    }
    }

    然后再你需要添加最热文章的地方,添加如下代码

    注:由于代码没有自适应模板,请自行根据模板style.css进行更改

    发表新评论