添加到当前主题的function.php
function getRandomPosts($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('RAND()')
);
if($result){
$i=1;
foreach($result as $val){
if($i<=3){
$var = ' class="red"';
}else{
$var = '';
}
$val = Typecho_Widget::widget('Widget_Abstract_Contents')->push($val);
$post_title = htmlspecialchars($val['title']);
$permalink = $val['permalink'];
echo '`<li><i'.$var.'>'.$i.'</i><a href="'.$permalink.'" title="'.$post_title.'" target="_blank">'.$post_title.'</a></li>`';
$i++;
}
}
}
前台调用
<?php getRandomPosts('10');?>
发表回复