Typecho随机调用文章

添加到当前主题的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');?>

已发布

分类

作者:

标签

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注