函数文档

is_time()

💡 云策文档标注

概述

is_time() 是一个 WordPress 条件标签函数,用于判断当前查询是否针对特定时间。它依赖于全局 $wp_query 对象,在查询运行前调用会返回 false 并触发错误提示。

关键要点

  • is_time() 返回布尔值,表示查询是否为特定时间(如时间归档页面)。
  • 函数内部检查 $wp_query 是否已设置,未设置时会调用 _doing_it_wrong() 并返回 false。
  • 相关函数包括 WP_Query::is_time()、__() 和 _doing_it_wrong(),用于查询判断、翻译和错误处理。
  • 自 WordPress 1.5.0 版本引入,是主题开发中常用的条件标签之一。

代码示例

if ( is_time() ) {
    // Do awesome.
}

注意事项

条件标签在查询运行前无效,调用时需确保 $wp_query 已初始化,否则可能返回错误结果。


📄 原文内容

Determines whether the query is for a specific time.

Description

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 a specific time.

Source

function is_time() {
	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_time();
}

Changelog

Version Description
1.5.0 Introduced.

User Contributed Notes