From 0017693c81f648779a609a8cc042eb486c1b5b15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=AA=E5=AD=90?= Date: Sat, 2 Aug 2025 14:29:25 +0800 Subject: [PATCH] 1.2.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加一个随机阅读的独立页面 --- README.MD | 3 ++- page-random.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 page-random.php diff --git a/README.MD b/README.MD index a41b7c8..9745e40 100644 --- a/README.MD +++ b/README.MD @@ -65,4 +65,5 @@ - 1.2.2 - 增加侧边栏显示的全局开关 - - 修复代码块中的短代码解析问题 \ No newline at end of file + - 修复代码块中的短代码解析问题 + - 新增一个随机文章阅读的独立页面 \ No newline at end of file diff --git a/page-random.php b/page-random.php new file mode 100644 index 0000000..0385412 --- /dev/null +++ b/page-random.php @@ -0,0 +1,43 @@ +select('COUNT(*) AS count') + ->from('table.contents') + ->where('status = ?', 'publish') + ->where('type = ?', 'post') + ->where('created <= ?', time()); +$countResult = $db->fetchRow($countSql); +$total = $countResult['count']; + +if ($total > 0) { + // 随机选择一个偏移量 + $offset = mt_rand(0, $total - 1); + + // 根据偏移量获取一篇文章 + $sql = $db->select() + ->from('table.contents') + ->where('status = ?', 'publish') + ->where('type = ?', 'post') + ->where('created <= ?', time()) + ->limit(1) + ->offset($offset); + + $result = $db->fetchRow($sql); + + if (!empty($result)) { + $target = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($result); + $this->response->redirect($target['permalink'], 307); + } +} + +// 如果没有找到文章,重定向到首页 +$this->response->redirect($this->options->siteUrl, 307); +?> \ No newline at end of file