函数文档

is_day()

💡 云策文档标注

概述

is_day() 是 WordPress 中的一个条件标签函数,用于检查当前查询是否为针对现有日归档页面的日期型归档查询。它基于全局 $wp_query 对象的状态返回布尔值,帮助主题开发者在模板中根据查询类型执行特定操作。

关键要点

  • is_day() 用于检测当前页面是否为显示当天文章的日期归档页面。
  • 函数返回布尔值:true 表示查询是针对现有日归档,false 表示不是。
  • 在查询运行前调用此函数会触发 _doing_it_wrong() 警告并返回 false,因为条件标签在查询执行前无效。
  • 函数内部调用 $wp_query->is_day() 来执行实际检查,依赖于全局 WP_Query 实例。

代码示例

if ( is_day() ) {
    // 如果是日归档页面,执行特定代码
}

注意事项

  • is_day() 必须在主查询运行后调用,否则会返回 false 并可能产生错误提示。
  • 此函数是 WordPress 条件标签的一部分,常用于主题开发中的归档页面逻辑控制。
  • 相关函数包括 WP_Query::is_day()、__() 和 _doing_it_wrong(),用于查询检查、翻译和错误处理。

📄 原文内容

Determines whether the query is for an existing day archive.

Description

A conditional check to test whether the page is a date-based archive page displaying posts for the current day.

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

Return

bool Whether the query is for an existing day archive.

Source

function is_day() {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
		return false;
	}

	return $wp_query->is_day();
}

Changelog

Version Description
1.5.0 Introduced.

User Contributed Notes