Typecho 随机文章(日志)函数

No Reply , Posted in 代码 on December 31, 2014

Typecho 随机文章调用代码,使用这个代码省的用插件了。
博主作为一个有洁癖的人,做模板能不用插件就尽量不用。

此代码加在functions.php

    function theme_random_posts(){
        $defaults = array(
            'number' => 5,
            'before' => '
    ', 'after' => '
', 'xformat' => '
  • {title}
  • ' ); $db = Typecho_Db::get(); $sql = $db->select()->from('table.contents') ->where('status = ?','publish') ->where('type = ?', 'post') ->limit($defaults['number']) ->order('RAND()'); $result = $db->fetchAll($sql); echo $defaults['before']; foreach($result as $val){ $val = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($val); echo str_replace(array('{permalink}', '{title}'),array($val['permalink'], $val['title']), $defaults['xformat']); } echo $defaults['after']; }

    调用代码:

    <?php theme_random_posts();?>
    

    标签: 随机文章