is_paged()
云策文档标注
概述
is_paged() 是一个 WordPress 条件标签函数,用于判断当前查询是否为分页结果且非第一页。它依赖于全局 $wp_query 对象,在查询运行前调用会返回 false 并触发错误提示。
关键要点
- is_paged() 返回布尔值,表示查询是否针对分页结果(非第一页)。
- 函数内部检查 $wp_query 是否已设置,未设置时会调用 _doing_it_wrong() 并返回 false。
- 相关函数包括 WP_Query::is_paged()、__() 和 _doing_it_wrong(),用于查询、翻译和错误处理。
- is_paged() 被多个函数如 get_custom_logo() 和 get_body_class() 使用,影响主题和模板功能。
- 自 WordPress 1.5.0 版本引入,是主题开发中处理分页逻辑的基础工具。
代码示例
function is_paged() {
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_paged();
}注意事项
在查询运行前调用 is_paged() 会触发 _doing_it_wrong() 警告并返回 false,开发者应确保在适当时机使用,如模板文件中。更多信息可参考主题开发者手册中的条件标签文章。
原文内容
Determines whether the query is for a paged result and not for the first page.
Description
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Source
function is_paged() {
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_paged();
}
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |