is_archive()
云策文档标注
概述
is_archive() 是 WordPress 的条件标签函数,用于判断当前查询是否为存档页面。它不接收任何参数,返回布尔值。
关键要点
- is_archive() 检查查询是否针对现有存档页面,包括分类、标签、作者、日期、自定义文章类型和自定义分类法存档。
- 该函数不接受参数;如需检查特定自定义文章类型存档,应使用 is_post_type_archive( $post_type )。
- 在查询运行前调用会返回 false,并触发 _doing_it_wrong() 警告。
- 函数内部调用 $wp_query->is_archive() 实现功能。
代码示例
if ( is_archive() ) {
// 当前页面是存档页面时的代码
}注意事项
- is_archive() 不适用于检查特定自定义文章类型存档,需使用 is_post_type_archive()。
- 确保在 WordPress 主查询运行后调用,否则可能返回错误结果。
原文内容
Determines whether the query is for an existing archive page.
Description
Archive pages include category, tag, author, date, custom post type, and custom taxonomy based archives.
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
See also
Source
function is_archive() {
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_archive();
}
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
Skip to note 2 content
Codex
Examples