var/Widget/Contents/Post目录新建Rand.php文件,内容如下

if (!defined('__TYPECHO_ROOT_DIR__')) exit;

class Widget_Contents_Post_Rand extends Widget_Abstract_Contents
{
/**
 * 执行函数
 *
 * @access public
 * @return void
 */
public function execute()
{
    $this->parameter->setDefault(array('pageSize' => $this->options->postsListSize));

    $select = $this->select();
    if(isset($this->request->mid)){
        $select->join('table.relationships', 'table.contents.cid = table.relationships.cid')
            ->where('table.relationships.mid = ?', $this->request->mid);
    }
    $select->where('table.contents.status = ?', 'publish')
        ->where('table.contents.created < ?', $this->options->gmtTime)
        ->where('table.contents.type = ?', 'post')
        ->order('', 'RAND()')
        ->limit($this->parameter->pageSize);

    $this->db->fetchAll($select, array($this, 'push'));
}
}

使用方法

    <div>
        <ul>
           <?php $this->widget('Widget_Contents_Post_Rand','pageSize=5')
                    ->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
        </ul>
 </div>

显示某个分类下的随机文章

       <div>
        <ul>
           <?php $this->widget('Widget_Contents_Post_Rand','pageSize=5','mid=3')
                   ->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
        </ul>
    </div>

此代码完美的解决了Single.主题,底部最新文章和归档页面的冲突问题.
转至:http://www.zhujimi.net/61.html