在當前主題的functions.php文件中添加如下函數代碼: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 '<li><a href="'.$permalink.'" title="'.$post_title.'" target="_blank">'.$post_title.'</a></li>'; } } }
在要調用熱評文章位置對應的模板文件(如index.php、single.php、sidebar.php或page.php等)添加調用代碼:html
<?php getHotComments('10');?>