WordPress添加「随机文章」功能

功能灵感来自博友Albus的博客装修日记,稍微研究了一下代码,很快就添加好了。

先添加随机文章的后台能力,将下列代码添加到当前主题的 functions.php 文件中:

function redirect_to_random_post() {
    if (isset($_GET['random_post']) && $_GET['random_post'] == 'true') {
        $random_post = get_posts(array(
            'orderby' => 'rand',
            'posts_per_page' => 1,
        ));

        if (!empty($random_post)) {
            wp_redirect(get_permalink($random_post[0]->ID));
            exit;
        }
    }
 }
add_action('template_redirect', 'redirect_to_random_post');

这里有一个进阶版本,可以设定随机内容的时间段,比如我只想随机2015年之后的文章,而且我不想包含906分类下的文章,那个分类是我发的status属性的博客,可以添加代码:

function redirect_to_random_post() {
    if (isset($_GET['random_post']) && $_GET['random_post'] == 'true') {
        $random_post = get_posts(array(
            'orderby' => 'rand',
            'posts_per_page' => 1,
            'date_query' => array(
                array(
                    'after' => '2015-01-01', // 仅获取 2015 年之后的文章
                    'inclusive' => true,    // 包括边界日期
                ),
            ),
            'category__not_in' => array(906), // 排除分类 ID 为 906 的文章
        ));

    if (!empty($random_post)) {
        wp_redirect(get_permalink($random_post[0]->ID));
        exit;
    }
}
}
add_action('template_redirect', 'redirect_to_random_post');

然后添加点击入口,可以通过超链接的方式放在网页的任何位置

<a href="<?php echo site_url(); ?>?random_post=true" class="random-button">随机漫游</a>

我在SVG网站找了个小图标,放在首页最上方,和当下实况(Now页面)一起,效果不错。顺便推荐一下这套Carbon Design Pictograms Collection

image-20250111101306264

补充一则:经 S 君提醒,添加后的代码不能支持黑暗模式,追加修改CSS代码以适应黑暗模式。之前的博客更新热力图也还一直没有适配黑暗模式,等有空再来研究。

.header--notice {
    display: flex;
    align-items: center;
    gap: 8px; /* 图标与文字的间距 */
    color: var(--hera-text-color, #000); /* 默认文本颜色 */
}

/* SVG 图标默认样式 */
.header--notice svg {
    display: inline-block;
    vertical-align: middle;
    fill: var(--hera-text-color, #000); /* 图标颜色,默认黑色 */
    filter: none; /* 白天模式不反色 */
}

/* 链接默认样式 */
.header--notice a {
    text-decoration: none;
    color: var(--hera-text-color, #000); /* 链接颜色,使用主题定义 */
    font-size: 16px;
}

/* 手动切换黑暗模式(.dark 类) */
.dark .header--notice {
    color: var(--hera-text-light, #a1a1aa); /* 黑暗模式文本颜色 */
}
.dark .header--notice svg {
    fill: var(--hera-text-light, #a1a1aa); /* 黑暗模式图标颜色 */
}
.dark .header--notice a {
    color: var(--hera-text-light, #a1a1aa); /* 黑暗模式链接颜色 */
}
/* 自动检测黑暗模式 */
@media (prefers-color-scheme: dark) {
    .auto .header--notice {
        color: var(--hera-text-light, #a1a1aa); /* 自动适配文本颜色 */
    }
    .auto .header--notice svg {
        fill: var(--hera-text-light, #a1a1aa); /* 自动适配图标颜色 */
    }
    .auto .header--notice a {
        color: var(--hera-text-light, #a1a1aa); /* 自动适配链接颜色 */
    }
}
2024年博客平台的选择
2024年博客平台的选择
flomo导入Obsidian或Onenote
flomo导入Obsidian或Onenote

4

  1. 山月

    :wink: 谢谢分享

    1. Leche

      :idea:

  2. S

    :mrgreen:

    1. Leche

      感谢 :oops:

Leave a Reply

Your email address will not be published. Required fields are marked *

废话 553065 字 · 622篇. Copyright © 2007-2024 Ouroboros.