Typecho Wiki
每一个作品都值得被记录

Typecho 随机跳转到一篇文章功能实现(手气不错)

Typecho维基君Typecho教程 • 3266次浏览 • 发布 2017-11-09 • 更新 2017-11-09
极致加速的V2Ray 助您畅享全球网络 & 搬瓦工VPS最新优惠码

文章很多时,读者不知道该看哪篇文章,我们可随机给读者显示一下片文章,在网上好的都是介绍如何在一个固定地方随机展示一下文章,今天给大家分享一种可以直接进到某文章页面的随机跳转。

利用MySQL的随机排列
首先是随机查询一篇文章出来。

<?php
$db = Typecho_Db::get();
$sql = $db->select()->from('table.contents')
    ->where('status = ?','publish')
    ->where('type = ?', 'post')
    ->where('created <= unix_timestamp(now())', 'post')
    ->limit(1)
    ->order('RAND()');
$result = $db->fetchAll($sql);
?>

PS:307是临时跳转的状态码

利用PHP的随机函数
然而昨天发现MySQL的查询有缓存,所以上边的方法很可能多次查询出来的文章是同一篇。
所以换个思路,直接查询文章cid的大小范围,然后直接在这个范围内取随机数,然后检测该随机数是否在cid列中存在,若存在,该cid是否对应的是文章,如果是,输出结果,不是,继续随机。

<?php
$db = Typecho_Db::get();
$sql = $db->select('MAX(cid)')->from('table.contents')
    ->where('status = ?','publish')
    ->where('type = ?', 'post')
    ->where('created <= unix_timestamp(now())', 'post');
$result = $db->fetchAll($sql);
$max_id = $result[0]['MAX(`cid`)'];//POST类型数据最大的CID
$sql = $db->select('MIN(cid)')->from('table.contents')
    ->where('status = ?','publish')
    ->where('type = ?', 'post')
    ->where('created <= unix_timestamp(now())', 'post');
$result = $db->fetchAll($sql);
$min_id = $result[0]['MIN(`cid`)'];//POST类型数据最小的CID
$result = NULL;
while($result == NULL) {
    $rand_id = mt_rand($min_id,$max_id);
    $sql = $db->select()->from('table.contents')
        ->where('status = ?','publish')
        ->where('type = ?', 'post')
        ->where('created <= unix_timestamp(now())', 'post')
        ->where('cid = ?',$rand_id);
    $result = $db->fetchAll($sql);
}
?>

使用方法
$result[0]就是我们取到的一篇文章。
如果你想直接展示(例如在404页面),请使用

<?php _e($result[0]['content']) ?>

不过我是想实现手气不错的功能,然而数据库中没有直接保存文章的链接,只有SLUG,所以得通过Typecho的内置函数转换一下。

<?php $target = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($result['0']); ?>

最后跳转即可。

<?php $this->response->redirect($target['permalink'],307); ?>

广告声明:文内含有的对外跳转链接(包括不限于超链接、二维码、口令等形式),用于传递更多信息,节省甄选时间,结果仅供参考,Typecho.Wiki所有文章均包含本声明。
本文检索关键词:typecho
厂商投放
添加新评论 »

已有 2 条评论 »

  1. 马佳 马佳

    站长好,我发现按照你的后者还是会有缓存,刷出来的仍是同一篇文章,站长能否修复一下?谢谢

    1. 我个人目前推荐的方法是从数据库文章中读取1000条出来并做缓存24小时,随机跳转就从这1000条缓存数据读取,这样做的好处是降低数据库频繁读取