is_year()
云策文档标注
概述
is_year() 是 WordPress 中的一个条件标签函数,用于判断当前查询是否为现有年份归档页面。它基于全局 $wp_query 对象,在查询运行后返回布尔值。
关键要点
- is_year() 检查查询是否为年份归档,返回 true 或 false。
- 函数依赖于 $wp_query 对象,在查询运行前调用会触发 _doing_it_wrong() 警告并返回 false。
- 相关函数包括 WP_Query::is_year()、__() 和 _doing_it_wrong(),用于查询、翻译和错误处理。
- 常用于主题开发中,如 wp_get_document_title() 和 get_the_archive_title() 函数。
代码示例
if ( is_year() ) {
// Do awesome.
}注意事项
条件标签在查询运行前无效,调用时需确保查询已执行,否则可能返回错误结果。
原文内容
Determines whether the query is for an existing year archive.
Description
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Source
function is_year() {
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_year();
}
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
Skip to note 2 content
Codex
Check if current archive is year archive.
if ( is_year() ) { // Do awesome. }