is_robots()
云策文档标注
概述
is_robots() 是一个 WordPress 条件标签函数,用于检测当前查询是否针对 robots.txt 文件。它返回布尔值,并依赖于 WP_Query 对象。
关键要点
- 函数 is_robots() 检查查询是否为 robots.txt 文件请求,返回 true 或 false。
- 必须在查询运行后调用,否则会触发 _doing_it_wrong() 警告并返回 false。
- 内部调用 WP_Query::is_robots() 方法执行实际检测。
- 相关函数包括 WP_Query::is_robots()、__() 和 _doing_it_wrong()。
- 自 WordPress 2.1.0 版本引入。
原文内容
Is the query for the robots.txt file?
Source
function is_robots() {
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_robots();
}
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |