Typecho热评文章的调用方法教程

Typecho教程 2453

热评文章即网站中被评论次数最多的文章,一般热评文章、热门文章和随机文章是网站常用的排行文章模块,那么typecho网站要如何在页面中显示评论最多的文章列表?很简单,代码博客吧已准备好,有需要的博主仅需要根据下面的说明添加即可。

操作步骤:

1、在当前主题的functions.php文件中添加以下函数代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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>';		
		}
	}
}

2、在要调用热评文章位置对应的模板文件(如index.php、single.php、sidebar.php或page.php等)添加调用代码:

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

代码中的10表示要调用的文章数量。

精品推荐: