欢迎光临
我们一直在努力

WordPress 随机文章时间

某些情况下,发表WordPress文章时不想让发表时间按当前的时间显示而是随机的,可以参考下面的代码。

将代码添加到当前主题函数模板functions.php中:

/**
 * 在指定时间范围内随机设置文章发布时间
 *
 * @param int    $post_id 文章ID
 * @param string $start_date 开始日期 (格式: Y-m-d H:i:s)
 * @param string $end_date 结束日期 (格式: Y-m-d H:i:s)
 */
function set_random_post_date( $post_id, $start_date, $end_date ) {
    // 将日期字符串转换为时间戳
    $start_timestamp = strtotime( $start_date );
    $end_timestamp   = strtotime( $end_date );

    // 生成随机时间戳
    $random_timestamp = rand( $start_timestamp, $end_timestamp );

    // 转换为MySQL日期时间格式
    $random_date = date( 'Y-m-d H:i:s', $random_timestamp );

    // 直接使用数据库更新以提高性能
    global $wpdb;
    $wpdb->update(
        $wpdb->posts,
        array(
            'post_date'     => $random_date,
            'post_date_gmt' => get_gmt_from_date( $random_date ),
        ),
        array( 'ID' => $post_id ),
        array( '%s', '%s' ),
        array( '%d' )
    );
}

/**
 * 批量设置文章随机发布时间
 */
function batch_set_random_post_dates() {
    $args = array(
        'post_type'      => 'post',
        'posts_per_page' => -1,
    );

    $posts = get_posts( $args );

    foreach ( $posts as $post ) {
        $start_date = get_option( 'start_date', '2023-01-01 00:00:00' );
        $end_date   = get_option( 'end_date', '2025-05-17 23:59:59' );
        set_random_post_date( $post->ID, $start_date, $end_date );
    }
}

// 添加管理菜单
add_action(
    'admin_menu',
    function () {
        add_options_page(
            '随机发布时间设置',
            '随机发布时间',
            'manage_options',
            'random-post-dates',
            function () {
                // 保存设置并执行批量设置
                if ( isset( $_POST['save_and_set'] ) ) {
                    // 保存设置
                    update_option( 'random_post_date_start', sanitize_text_field( $_POST['start_date'] ) );
                    update_option( 'random_post_date_end', sanitize_text_field( $_POST['end_date'] ) );
                    update_option( 'random_post_date_enabled', isset( $_POST['enabled'] ) ? '1' : '0' );
                    update_option( 'random_post_date_update_enabled', isset( $_POST['update_enabled'] ) ? '1' : '0' );

                    // 执行批量设置
                    batch_set_random_post_dates();
                    echo '<div class="updated"><p>设置已保存</p></div>';
                }

                // 获取当前设置
                $start_date     = get_option( 'random_post_date_start', '2023-01-01 00:00:00' );
                $end_date       = get_option( 'random_post_date_end', '2025-05-17 23:59:59' );
                $enabled        = get_option( 'random_post_date_enabled', '1' );
                $update_enabled = get_option( 'random_post_date_update_enabled', '0' );
                ?>
            <div class="wrap">
                <h2>随机发布时间设置</h2>
                <form method="post">
                    <table class="form-table">
                        <tr>
                            <th scope="row">新文章随机时间</th>
                            <td>
                                <label>
                                    <input type="checkbox" name="enabled" value="1" <?php checked( $enabled, '1' ); ?>>
                                    启用
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">更新文章随机时间</th>
                            <td>
                                <label>
                                    <input type="checkbox" name="update_enabled" value="1" <?php checked( $update_enabled, '1' ); ?>>
                                    启用
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">开始时间</th>
                            <td>
                                    <input type="text" name="start_date" value="<?php echo esc_attr( $start_date ); ?>" class="regular-text">
                                <p class="description">格式:YYYY-MM-DD HH:mm:ss</p>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">结束时间</th>
                            <td>
                                    <input type="text" name="end_date" value="<?php echo esc_attr( $end_date ); ?>" class="regular-text">
                                <p class="description">格式:YYYY-MM-DD HH:mm:ss</p>
                            </td>
                        </tr>
                    </table>
                    <p class="submit">
                        <input type="submit" name="save_and_set" class="button-primary" value="保存设置">
                    </p>
                </form>
            </div>
                <?php
            }
        );
    }
);

// 修改发布和更新文章时的随机时间设置
add_action(
    'transition_post_status',
    function ( $new_status, $old_status, $post ) {
        // 检查是否为文章类型
        if ( $post->post_type !== 'post' ) {
            return;
        }

        // 检查是否为发布或更新操作
        if ( $new_status === 'publish' ) {
            // 如果是从非发布状态到发布状态(新发布)
            if ( $old_status !== 'publish' ) {
                // 检查新发布功能是否启用
                if ( get_option( 'random_post_date_enabled', '1' ) === '1' ) {
                    $start_date = get_option( 'random_post_date_start', '2023-01-01 00:00:00' );
                    $end_date   = get_option( 'random_post_date_end', '2025-05-17 23:59:59' );
                    set_random_post_date( $post->ID, $start_date, $end_date );
                }
            }
            // 如果是已发布文章的更新
            else {
                // 检查更新时随机时间功能是否启用
                if ( get_option( 'random_post_date_update_enabled', '0' ) === '1' ) {
                    $start_date = get_option( 'random_post_date_start', '2023-01-01 00:00:00' );
                    $end_date   = get_option( 'random_post_date_end', '2025-05-17 23:59:59' );
                    set_random_post_date( $post->ID, $start_date, $end_date );
                }
            }
        }
    },
    10,
    3
);

之后进入设置 → 随机发布时间设置,可以分别设置新发表文章或更新文章时随机时间。

赞(0) 打赏
未经允许不得转载:WORDPRESS大侠 » WordPress 随机文章时间

评论 抢沙发

评论前必须登录!

 

更好的WordPress主题

支持快讯、专题、百度收录推送、人机验证、多级分类筛选器,适用于垂直站点、科技博客、个人站,扁平化设计、简洁白色、超多功能配置、会员中心、直达链接、文章图片弹窗、自动缩略图等...

联系我们联系我们

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册