聯係
第一步:在 functions.php 中添加获取随机文章的函数
打开主题的 functions.php 文件,在文件最末尾的 } 上方(或者直接在文件底部,只要处于 <?php ... ?> 标签内),添加以下获取随机文章的代码:
PHP
/**
* 获取随机文章列表
* * @param int $limit 显示数量
* @return array 文章列表数据
*/
function shufei_get_random_posts($limit = 5) {
$db = \Typecho\Db::get();
// 配合 MySQL 的 ORDER BY RAND() 随机排序
$select = $db->select()->from('table.contents')
->where('status = ?', 'publish')
->where('created < ?', \Typecho\Date::time())
->where('type = ?', 'post')
->order('RAND()')
->limit($limit);
return $db->fetchAll($select);
}
第二步:在右侧边栏添加随机文章 HTML 结构
你需要分别修改 index.php、post.php 和 page.php。在这三个文件中找到右侧边栏的末尾(即