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');?>

声明:Jefsky-程序猿甜品店|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - Typecho随机调用文章


程序猿甜品店