WordPress函数:is_date 判断是否日期归档页

追格官方小助手/ 2022年02月16日/ WordPress/ 浏览 1506

只看is_date这个函数的名字,很容易误认为是判断是否是时间类型的。

其实,在WordPress中,is_date是用来判断是否是日期归档页的。类似的函数还有:is_year、is_month、is_day。

is_year对应的链接类似:.../date/2022。

is_month对应的链接类似:.../date/2022/02。

is_day对应的链接类似:.../date/2022/02/18。

此函数经常于 wp_get_archives、get_year_link、get_month_link、get_day_link 配合,生成全站的按日期归档的页面date.php。

用法举例:

if (is_date()) {
    if (is_year()) {
        echo get_the_time('Y');
    } else if (is_month()) {
        echo get_the_time('Y-m');
    } else if (is_day()) {
        echo get_the_time('Y-m-d');
    }
}

发表评论

暂无评论,抢个沙发...

客服 工单