修改主题 functions.php 文件

控制台 / 外观 / 编辑当前外观 / 在 functions.php 加入以下函数代码

function randomPost($type='echo') {
    $db = Typecho_Db::get();
    $result = $db->fetchRow($db->select()->from('table.contents')->where('type=?', 'post')->where('status=?', 'publish')->limit(1)->order('RAND()'));
    if($result) {
        $f=Helper::widgetById('Contents',$result['cid']);
        $permalink = $f->permalink;
        if($type=="return"){return $permalink;}else{echo $permalink;}
    } else {
        if($type=="return"){return false;}else{echo "没有文章可随机";}
    }
}

function themeInit($archive)
{
if($archive->request->isGet() && $archive->request->get('random')){
    header('Location: '.randomPost('return'));exit;
}
}
直接调用 randomPost() 即可输出随机出来的文章地址,使用 randomPost("return") 可返回随机到的文章地址。

如何使用?

在需要的地方添加超链接 你的域名?random=true 即可随机访问你的文章。