is_feed()
云策文档标注
概述
is_feed() 是一个 WordPress 条件标签函数,用于判断当前查询是否为 feed 请求。它基于全局 $wp_query 对象,并支持检查特定 feed 类型。
关键要点
- 函数用于检测查询是否为 feed,返回布尔值。
- 接受可选参数 $feeds,可以是字符串或字符串数组,用于指定要检查的 feed 类型(如 'rss2'),默认为空。
- 在查询运行前调用会触发 _doing_it_wrong() 警告并返回 false。
- 内部调用 WP_Query::is_feed() 方法实现功能。
代码示例
function is_feed( $feeds = '' ) {
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_feed( $feeds );
}注意事项
- 此函数属于条件标签,应在查询运行后使用,否则可能返回错误结果。
- 更多信息可参考 Theme Developer Handbook 中的 Conditional Tags 文章。
原文内容
Determines whether the query is for a feed.
Description
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Parameters
$feedsstring|string[]optional-
Feed type or array of feed types to check against. Default empty.
Source
function is_feed( $feeds = '' ) {
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_feed( $feeds );
}
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |