美好的明天

分享改变,记录生活
美好的明天
当前位置: 首页 > CoreNext, WordPress > 正文

前言

因为女儿的出生一直想做一个纪念日页面,但苦于一直都没有好一点的美化页面思路 ~

偶然间搜索到别碰我的镜头盖制作的喵喵纪念日页面很漂亮,但作者博客是基于Hexo博客框架,不能直接使用,本文修改后将适用于WordPressCoreNext主题。

其他WordPress主题需自行二次修改才可使用,使用前请自行测试;

因本人技术有限,部分做的还不合理,如有更好的实现方法欢迎交流学习 😊。

作者文章链接:给你的网站添加喵喵纪念日页面 | 别碰我的镜头盖

本站纪念日页面链接:纪恋日 | 美好的明天

25-1-11:已修改为支持CoreNext-Child子主题,使用本页面介绍的新方法,后续更新主题版本此部分修改不会丢失。

 

效果演示

 

修改步骤

注意事项

以下修改方法仅适用于 CoreNext 的子主题 CoreNext-Child,安装方法点此查看,此处不再赘述。

由于不同主题的代码结构、样式设定以及功能逻辑存在显著差异,此方法无法确保在其他主题上直接可用。

倘若你希望在其他主题中应用,务必依据对应主题的特性自行修改、适配代码,以实现预期效果。

在操作前,请务必备份好原文件,以免出现不可挽回的错误,导致网站显示异常或功能缺失。

 

1 创建 anniversary.css

创建anniversary.css,将以下代码粘贴入文件内,上传到自定义位置,并记住此路径,下一步需要引用;

body[data-type=anniversary] #web_bg{background:var(--anzhiyu-background)}
body[data-type=anniversary] #page{border:0;-webkit-box-shadow:none!important;box-shadow:none!important;padding:0!important;background:0 0!important}
body[data-type=anniversary] #page .page-title{display:none}
.anniversary-cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(250px,1fr));gap:20px;padding:20px}
.anniversary-card{background-color:#f9f9f9;border-radius:10px;padding:20px;box-shadow:0 4px 6px rgba(0,0,0,.1);transition:transform .3s ease-in-out,box-shadow .3s ease-in-out}
.anniversary-card:hover{transform:translateY(-10px);box-shadow:0 10px 20px rgba(0,0,0,.1)}
.card-header{display:flex;justify-content:center;align-items:center;margin-bottom:10px}
.card-title{font-size:1.5rem;font-weight:700;color:#333;text-align:center}
.card-icon{width:24px;height:24px;margin-right:10px}
.card-body{background-color:#fff;padding:20px;margin-bottom:10px;border-radius:8px;color:#333;text-align:center}
.countdown-wrapper{position:relative;display:inline-block;font-size:3rem;color:#6b4226;font-weight:700}
.days-label{position:absolute;top:-10px;right:-45px;background-color:#36c5b2;color:#fff;padding:3px 10px;border-radius:20px;font-size:1rem;font-weight:400;display:inline-block}
.target-info{text-align:center;font-size:1rem;font-weight:400}
.target-label{display:inline-block;margin:0;font-size:1rem;letter-spacing:2px}
.target-date{display:inline-block;font-size:1rem;color:#333;margin:0;font-weight:400}
.dashed-line{border-top:1px dashed #ccc;margin:10px 0}
.target-info,.total-days-info{font-size:1rem;margin:5px 0;color:#555}
@media (max-width:768px){.anniversary-cards{grid-template-columns:1fr}
.anniversary-card{padding:15px}
.card-title{font-size:1.2rem}
}
.anniversary-card{transition:all .3s ease-in-out}
.anniversary-card:hover{transform:scale(1.05);box-shadow:0 6px 20px rgba(0,0,0,.15)}
.card-copyright{margin-top:10px;font-size:.85rem;color:#777;text-align:right}
.card-copyright a{color:#007bff;text-decoration:none}
.card-copyright a:hover{text-decoration:underline}
.anniversary-group{margin-bottom:40px}
.group-header{text-align:left;margin-bottom:10px}
.group-title{font-size:1.5rem;font-weight:700;margin-bottom:5px;text-align:left}
.group-desc{font-size:1rem;color:#666;margin-bottom:20px;text-align:left}

 

2 创建 page-commemoration.php

找到/CoreNext-Child/template/文件夹,在此文件夹下创建page-commemoration.php文件,将如下代码复制到刚刚创建的page-commemoration.php文件内;

<?php
namespace core_next;
the_post(); ?>

<!doctype html>
<html lang="zh">
<?php get_template_part("template/views/header"); ?>
    
    <div class="content-warp">
        <div class="server-time">
<?php
// 设置时区为东八区(Asia/Shanghai)
date_default_timezone_set('Asia/Shanghai');

// 定义存储中文星期表述的数组,用于将星期数字转换为中文表示
$weekday_chinese = array('日', '一', '二', '三', '四', '五', '六');

// 定义获取下一次生日时间戳的函数,用于处理生日相关逻辑
function getNextBirthdayTimestamp($birth_month, $birth_day) {
    $current_date = time();
    $this_year_birth_date = strtotime(date('Y'). '-'. $birth_month. '-'. $birth_day);
    if ($this_year_birth_date < $current_date) {
        return strtotime((date('Y') + 1). '-'. $birth_month. '-'. $birth_day);
    }
    return $this_year_birth_date;
}

// 获取当前的 Unix 时间戳并赋值给变量 $current_date
$current_date = time();

// 女儿出生纪念日相关计算
$birth_date = strtotime('2024-6-29');
$diff_seconds_birth = $current_date - $birth_date;
$days_since_birth = floor($diff_seconds_birth / 86400);
$weeks_since_birth = floor($days_since_birth / 7);
$months_since_birth = floor($days_since_birth / 30.4368);
$years_since_birth = floor($days_since_birth / 365);
$weekday_num_birth = date('w', $birth_date);
$weekday_birth = '星期'. $weekday_chinese[$weekday_num_birth];

// 恋爱纪念日相关计算
$meet_date = strtotime('2023-3-30');
$diff_seconds_meet = $current_date - $meet_date;
$days_since_meet = floor($diff_seconds_meet / 86400);
$weekday_num_meet = date('w', $meet_date);
$weekday_meet = '星期'. $weekday_chinese[$weekday_num_meet];
$start_year = date('Y', $meet_date);
$current_year = date('Y', $current_date);
$years_since_meet = $current_year - $start_year;

// 结婚纪念日相关计算
$wedding_date = strtotime('2024-1-6');
$diff_seconds_wedding = $current_date - $wedding_date;
$days_since_wedding = floor($diff_seconds_wedding / 86400);
$weekday_num_wedding = date('w', $wedding_date);
$weekday_wedding = '星期'. $weekday_chinese[$weekday_num_wedding];

// 老婆生日相关计算
$wife_birth_date = getNextBirthdayTimestamp('10', '19');
$diff_seconds_wife_birth = $wife_birth_date - $current_date;
$days_until_wife_birth = floor($diff_seconds_wife_birth / 86400);
$weekday_num_wife_birth = date('w', $wife_birth_date);
$weekday_wife_birth = '星期'. $weekday_chinese[$weekday_num_wife_birth];

// 女儿生日相关计算
$daughter_birth_date = getNextBirthdayTimestamp('6', '29');
$diff_seconds_daughter_birth = $daughter_birth_date - $current_date;
$days_until_daughter_birth = floor($diff_seconds_daughter_birth / 86400);
$weekday_num_daughter_birth = date('w', $daughter_birth_date);
$weekday_daughter_birth = '星期'. $weekday_chinese[$weekday_num_daughter_birth];

// 我的生日相关计算
$my_birth_date = getNextBirthdayTimestamp('7', '16');
$diff_seconds_my_birth = $my_birth_date - $current_date;
$days_until_my_birth = floor($diff_seconds_my_birth / 86400);
$weekday_num_my_birth = date('w', $my_birth_date);
$weekday_my_birth = '星期'. $weekday_chinese[$weekday_num_my_birth];

// 妈妈生日相关计算
$mom_birth_date = getNextBirthdayTimestamp('4', '2');
$diff_seconds_mom_birth = $mom_birth_date - $current_date;
$days_until_mom_birth = floor($diff_seconds_mom_birth / 86400);
$weekday_num_mom_birth = date('w', $mom_birth_date);
$weekday_mom_birth = '星期'. $weekday_chinese[$weekday_num_mom_birth];

// 爸爸生日相关计算
$dad_birth_date = getNextBirthdayTimestamp('10', '15');
$diff_seconds_dad_birth = $dad_birth_date - $current_date;
$days_until_dad_birth = floor($diff_seconds_dad_birth / 86400);
$weekday_num_dad_birth = date('w', $dad_birth_date);
$weekday_dad_birth = '星期'. $weekday_chinese[$weekday_num_dad_birth];
?>

            <h2>💖 女儿的出生纪念日</h2>
            <div class="anniversary-cards">
                <div class="anniversary-card" style="background-color: #fcbad3">
                    <div class="card-content">
                        <div class="card-header" style="justify-content: center;">
                            <div class="card-title">👶 女儿</div>
                        </div>
                        <div class="card-body" style="background-color: white; padding: 20px; text-align: center;">
                            <div class="countdown-wrapper">
                                <span class="countdown" data-date="2024-6-29" data-display-mode="remaining">
                                    <?php echo $days_since_birth;?>
                                </span>
                                <div class="days-label">天啦</div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <div class="dashed-line" style="border-top: 1px dashed #ccc; margin: 10px 0;">
                                <!-- 虚线 -->
                            </div>
                            <div class="target-info">
                                <span class="target-label">- 起始日 -</span>
                                <br>
                                <span class="target-date" data-date="2024-6-29" data-display-mode="remaining">
                                    <?php echo date('Y-m-d', $birth_date).' ('.$weekday_birth.')';?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="anniversary-card" style="background-color: #fcbad3">
                    <div class="card-content">
                        <div class="card-header" style="justify-content: center;">
                            <div class="card-title">👶 女儿</div>
                        </div>
                        <div class="card-body" style="background-color: white; padding: 20px; text-align: center;">
                            <div class="countdown-wrapper">
                                <span class="countdown" data-date="2024-6-29" data-display-mode="remaining">
                                    <?php echo $weeks_since_birth;?>
                                </span>
                                <div class="days-label">周啦</div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <div class="dashed-line" style="border-top: 1px dashed #ccc; margin: 10px 0;"></div>
                            <div class="target-info">
                                <span class="target-label">- 起始日 -</span>
                                <br>
                                <span class="target-date" data-date="2024-6-29" data-display-mode="remaining">
                                    <?php echo date('Y-m-d', $birth_date).' ('.$weekday_birth.')';?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="anniversary-card" style="background-color: #fcbad3">
                    <div class="card-content">
                        <div class="card-header" style="justify-content: center;">
                            <div class="card-title">👶 女儿</div>
                        </div>
                        <div class="card-body" style="background-color: white; padding: 20px; text-align: center;">
                            <div class="countdown-wrapper">
                                <span class="countdown" data-date="2024-6-29" data-display-mode="remaining">
                                    <?php echo $months_since_birth;?>
                                </span>
                                <div class="days-label">月啦</div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <div class="dashed-line" style="border-top: 1px dashed #ccc; margin: 10px 0;"></div>
                            <div class="target-info">
                                <span class="target-label">- 起始日 -</span>
                                <br>
                                <span class="target-date" data-date="2024-6-29" data-display-mode="remaining">
                                    <?php echo date('Y-m-d', $birth_date).' ('.$weekday_birth.')';?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <h2>💖 我们的恋爱纪恋日</h2>
            <div class="anniversary-cards">
                <div class="anniversary-card" style="background-color: #99ddcc">
                    <div class="card-content">
                        <div class="card-header" style="justify-content: center;">
                            <div class="card-title">💕 我们相识已经</div>
                        </div>
                        <div class="card-body" style="background-color: white; padding: 20px; text-align: center;">
                            <div class="countdown-wrapper">
                                <span class="countdown" data-date="2023-3-30" data-display-mode="remaining">
                                    <?php echo $days_since_meet;?>
                                </span>
                                <div class="days-label">天了</div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <div class="dashed-line" style="border-top: 1px dashed #ccc; margin: 10px 0;"></div>
                            <div class="target-info">
                                <span class="target-label">- 起始日 -</span>
                                <br>
                                <span class="target-date" data-date="2023-3-30" data-display-mode="remaining">
                                    <?php echo date('Y-m-d', $meet_date).' ('.$weekday_meet.')';?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="anniversary-card" style="background-color: #ffe2e2">
                    <div class="card-content">
                        <div class="card-header" style="justify-content: center;">
                            <div class="card-title">💕 我们相识已经</div>
                        </div>
                        <div class="card-body" style="background-color: white; padding: 20px; text-align: center;">
                            <div class="countdown-wrapper">
                                <span class="countdown" data-date="2023-3-30" data-display-mode="remaining">
                                    <?php echo $years_since_meet;?>
                                </span>
                                <div class="days-label">年了</div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <div class="dashed-line" style="border-top: 1px dashed #ccc; margin: 10px 0;"></div>
                            <div class="target-info">
                                <span class="target-label">- 起始日 -</span>
                                <br>
                                <span class="target-date" data-date="2023-3-30" data-display-mode="remaining">
                                    <?php echo date('Y-m-d', $meet_date).' ('.$weekday_meet.')';?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="anniversary-card" style="background-color: #ffb3c6">
                    <div class="card-content">
                        <div class="card-header" style="justify-content: center;">
                            <div class="card-title">💒 结婚</div>
                        </div>
                        <div class="card-body" style="background-color: white; padding: 20px; text-align: center;">
                            <div class="countdown-wrapper">
                                <span class="countdown" data-date="2024-01-06" data-display-mode="remaining">
                                    <?php echo $days_since_wedding;?>
                                </span>
                                <div class="days-label">天了</div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <div class="dashed-line" style="border-top: 1px dashed #ccc; margin: 10px 0;"></div>
                            <div class="target-info">
                                <span class="target-label">- 起始日 -</span>
                                <br>
                                <span class="target-date" data-date="2024-01-06" data-display-mode="remaining">
                                    <?php echo date('Y-m-d', $wedding_date).' ('.$weekday_wedding.')';?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <h2>👨‍👩‍👧 我们的生日</h2>
            <div class="anniversary-cards">
                <div class="anniversary-card" style="background-color: #defcf9">
                    <div class="card-content">
                        <div class="card-header" style="justify-content: center;">
                            <div class="card-title">🎂 老婆生日</div>
                        </div>
                        <div class="card-body" style="background-color: white; padding: 20px; text-align: center;">
                            <div class="countdown-wrapper">
                                <span class="countdown" data-date="<?php echo date('Y-m-d', $wife_birth_date);?>" data-display-mode="remaining">
                                    <?php echo $days_until_wife_birth;?>
                                </span>
                                <div class="days-label">天后</div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <div class="dashed-line" style="border-top: 1px dashed #ccc; margin: 10px 0;"></div>
                            <div class="target-info">
                                <span class="target-label">- 目标日 -</span>
                                <br>
                                <span class="target-date" data-date="<?php echo date('Y-m-d', $wife_birth_date);?>" data-display-mode="remaining">
                                    <?php echo date('Y-m-d', $wife_birth_date).' ('.$weekday_wife_birth.')';?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="anniversary-card" style="background-color: #cadefc">
                    <div class="card-content">
                        <div class="card-header" style="justify-content: center;">
                            <div class="card-title">🍰 女儿生日</div>
                        </div>
                        <div class="card-body" style="background-color: white; padding: 20px; text-align: center;">
                            <div class="countdown-wrapper">
                                <span class="countdown" data-date="<?php echo date('Y-m-d', $daughter_birth_date);?>" data-display-mode="remaining">
                                    <?php echo $days_until_daughter_birth;?>
                                </span>
                                <div class="days-label">天后</div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <div class="dashed-line" style="border-top: 1px dashed #ccc; margin: 10px 0;"></div>
                            <div class="target-info">
                                <span class="target-label">- 目标日 -</span>
                                <br>
                                <span class="target-date" data-date="<?php echo date('Y-m-d', $daughter_birth_date);?>" data-display-mode="remaining">
                                    <?php echo date('Y-m-d', $daughter_birth_date).' ('.$weekday_daughter_birth.')';?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="anniversary-card" style="background-color: #c3bef0">
                    <div class="card-content">
                        <div class="card-header" style="justify-content: center;">
                            <div class="card-title">🍨 我的生日</div>
                        </div>
                        <div class="card-body" style="background-color: white; padding: 20px; text-align: center;">
                            <div class="countdown-wrapper">
                                <span class="countdown" data-date="<?php echo date('Y-m-d', $my_birth_date);?>" data-display-mode="remaining">
                                    <?php echo $days_until_my_birth;?>
                                </span>
                                <div class="days-label">天后</div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <div class="dashed-line" style="border-top: 1px dashed #ccc; margin: 10px 0;"></div>
                            <div class="target-info">
                                <span class="target-label">- 目标日 -</span>
                                <br>
                                <span class="target-date" data-date="<?php echo date('Y-m-d', $my_birth_date);?>" data-display-mode="remaining">
                                    <?php echo date('Y-m-d', $my_birth_date).' ('.$weekday_my_birth.')';?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <h2>🎂 家人的生日</h2>
            <div class="anniversary-cards">
                <div class="anniversary-card" style="background-color: #f1faee">
                    <div class="card-content">
                        <div class="card-header" style="justify-content: center;">
                            <div class="card-title">👩 妈妈生日</div>
                        </div>
                        <div class="card-body" style="background-color: white; padding: 20px; text-align: center;">
                            <div class="countdown-wrapper">
                                <span class="countdown" data-date="<?php echo date('Y-m-d', $mom_birth_date);?>" data-display-mode="remaining">
                                    <?php echo $days_until_mom_birth;?>
                                </span>
                                <div class="days-label">天后</div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <div class="dashed-line" style="border-top: 1px dashed #ccc; margin: 10px 0;"></div>
                            <div class="target-info">
                                <span class="target-label">- 目标日 -</span>
                                <br>
                                <span class="target-date" data-date="<?php echo date('Y-m-d', $mom_birth_date);?>" data-display-mode="remaining">
                                    <?php echo date('Y-m-d', $mom_birth_date).' ('.$weekday_mom_birth.')';?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="anniversary-card" style="background-color: #f1faee">
                    <div class="card-content">
                        <div class="card-header" style="justify-content: center;">
                            <div class="card-title">👨 爸爸生日</div>
                        </div>
                        <div class="card-body" style="background-color: white; padding: 20px; text-align: center;">
                            <div class="countdown-wrapper">
                                <span class="countdown" data-date="<?php echo date('Y-m-d', $dad_birth_date);?>" data-display-mode="remaining">
                                    <?php echo $days_until_dad_birth;?>
                                </span>
                                <div class="days-label">天后</div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <div class="dashed-line" style="border-top: 1px dashed #ccc; margin: 10px 0;"></div>
                            <div class="target-info">
                                <span class="target-label">- 目标日 -</span>
                                <br>
                                <span class="target-date" data-date="<?php echo date('Y-m-d', $dad_birth_date);?>" data-display-mode="remaining">
                                    <?php echo date('Y-m-d', $dad_birth_date).' ('.$weekday_dad_birth.')';?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <p style="text-align: right;">灵感来源:<a href="//www.wmviv.com/go.php?url=https://blog.bornforthis.cn/posts/41c7c45e.html"  target="_blank" rel="noopener">给你的网站添加喵喵纪念日页面 | 别碰我的镜头盖</a></p>
            <link rel="stylesheet" type="text/css" href="https://static.wmviv.com/www/calendar/calendar.css">
        </div>
    </div>
</div>
<app-comment id="app-comment"></app-comment>
</div>
<aside>
<?php dynamic_sidebar("page_sidebar"); ?>
</aside>
</main>
<?php get_template_part("template/module/footer"); ?>
</div>
<?php get_template_part("template/module/body-end"); ?>
</body>
<?php wp_footer(); ?>
</html>

复制后上传前,搜索https://static.wmviv.com/www/calendar/calendar.css,将此路径修改为上一步自己创建anniversary.css的路径;

 

纪念日部分代码详细解释

纪念日部分代码切片,以下使用恋爱纪念日部分作为示例:

// 恋爱纪念日相关计算
// 将字符串格式的恋爱纪念日日期 '2023-3-30' 转换为时间戳,以便后续能与当前时间进行时间差等相关运算。
// strtotime函数会把这个日期解析为从1970年1月1日00:00:00 UTC到2023年3月30日00:00:00的秒数,方便后续的时间计算操作。
$meet_date = strtotime('2023-3-30');

// 计算从当前时间到恋爱纪念日的时间差,单位为秒。
// 用当前时间的时间戳(之前已通过time()函数获取并存放在$current_date变量中)减去恋爱纪念日的时间戳,得到两者间隔的秒数。
$diff_seconds_meet = $current_date - $meet_date;

// 将时间差(秒数)换算为天数,以86400秒为一天进行换算(因为一天包含24小时,每小时3600秒,24 * 3600 = 86400)。
// 使用floor函数进行向下取整操作,确保得到的天数是一个整数,也就是获取从恋爱纪念日到当前时间经过了多少整天。
$days_since_meet = floor($diff_seconds_meet / 86400);

// 获取恋爱纪念日对应的星期数字,date函数在这里使用 'w' 参数,其作用是返回该日期对应的星期几的数字表示。
// 其中0表示星期日,1到6分别对应周一到周六,通过这个函数调用可以知道恋爱纪念日当天是星期几。
$weekday_num_meet = date('w', $meet_date);

// 根据获取到的恋爱纪念日的星期数字,从之前定义好的$weekday_chinese数组中取出对应的中文星期表述,然后拼接成“星期X”的格式。
// 例如,如果$weekday_num_meet的值是2,那么就会从$weekday_chinese数组中取出“二”,最终$weekday_meet的值就是“星期二”。
$weekday_meet = '星期'. $weekday_chinese[$weekday_num_meet];

// 获取恋爱纪念日所在的年份,通过date函数使用 'Y' 参数,它会返回对应日期的四位年份数字,这里就是获取恋爱纪念日的年份信息,比如“2023”。
$start_year = date('Y', $meet_date);

// 获取当前时间所在的年份,同样使用date函数搭配 'Y' 参数,获取到当前时刻对应的年份数字,例如“2025”等(取决于实际运行代码时的当前年份)。
$current_year = date('Y', $current_date);

// 计算从恋爱纪念日到当前时间经过了多少年,通过用当前年份减去恋爱纪念日所在的年份,得到一个简单的年数差值,以此来表示经过了多少年。
$years_since_meet = $current_year - $start_year;

 

生日部分代码详细解释

生日部分代码切片,以下使用我的生日部分作为示例:

// 我的生日相关计算
// 调用getNextBirthdayTimestamp函数来获取下一次我的生日对应的时间戳。
// 传入'7'作为生日所在月份(代表7月),'16'作为生日所在日,该函数会依据当前时间判断是今年还是明年的生日时间,并返回对应的时间戳,将其赋值给$my_birth_date变量。
$my_birth_date = getNextBirthdayTimestamp('7', '16');

// 计算距离我下一次生日还有多少秒,通过用下一次我的生日时间戳(存储在$my_birth_date变量中)减去当前时间的时间戳(之前已通过time()函数获取并存放在$current_date变量中),得到时间差(秒数),表示距离生日还有多久(以秒为单位)。
$diff_seconds_my_birth = $my_birth_date - $current_date;

// 将距离我下一次生日的时间差(秒数)换算为天数,以86400秒为一天进行换算(因为一天包含24小时,每小时3600秒,24 * 3600 = 86400)。
// 使用floor函数进行向下取整操作,确保得到的天数是一个整数,也就是获取距离我下一次生日还有多少整天。
$days_until_my_birth = floor($diff_seconds_my_birth / 86400);

// 获取我下一次生日对应的星期数字,date函数在这里使用 'w' 参数,其作用是返回该日期对应的星期几的数字表示。
// 其中0表示星期日,1到6分别对应周一到周六,通过这个函数调用可以知道我下一次生日当天是星期几。
$weekday_num_my_birth = date('w', $my_birth_date);

// 根据获取到的我下一次生日的星期数字,从之前定义好的$weekday_chinese数组中取出对应的中文星期表述,然后拼接成“星期X”的格式。
// 例如,如果$weekday_num_my_birth的值是3,那么就会从$weekday_chinese数组中取出“三”,最终$weekday_my_birth的值就是“星期三”。
$weekday_my_birth = '星期'. $weekday_chinese[$weekday_num_my_birth];

 

3 修改functions.php文件

打开/CoreNext-Child/functions.php文件,将如下代码复制到/CoreNext-Child/functions.php文件<?php

/*-----------------------------------------------------------------------------------*/

// 页面增加纪念日模板页 [personalInformation]
/*-----------------------------------------------------------------------------------*/

function commemoration() {
    $post_templates = ["core_next_page_wmviv_commemoration" => "[CoreNext]纪恋日"];
    add_filter('page_template', function ($page_template) {
        $slug = get_page_template_slug();
        return ($slug == "core_next_page_wmviv_commemoration")? get_stylesheet_directory(). '/template/page-commemoration.php' : $page_template;
    });
    add_filter('theme_page_templates', function ($templates) use ($post_templates) {
        $templates = array_merge($templates, $post_templates);
        return $templates;
    }, 10, 1);
}
add_action('init', 'commemoration');

保存并上传

 

使用方法

登录后台页面,点击需要引用此模板的文章,选择快速编辑,在右下角模板选择[CoreNext]纪恋日,最后更新即可。

温馨提示
本页面最后更新于:2025-01-31 00:12:51,距今已 89 天,若有链接失效或教程无效,欢迎留言反馈。
THE END

发表评论

gravatar

OK aixin aoman baoquan bizui cahan caidao ciya dabing doge fadai ganga guzhang haixiu hanxiao huaixiao jie jingkong keai koubi ku leiben lenghan liuhan nanguo penxue piezui qiang qinqin quantou se shengli shuai tiaopi touxiao tuosai weiqu woshou wozuimei wunai xiaojiujie xiaoku xieyanxiao xigua yinxian yiwen youling yun