is_privacy_policy()
云策文档标注
概述
is_privacy_policy() 是一个 WordPress 条件标签函数,用于判断当前查询是否为隐私政策页面。它依赖于站点设置中的“隐私政策页面”选项,仅在指定页面返回 true。
关键要点
- 函数用于检测查询是否针对隐私政策页面,返回布尔值。
- 依赖于 'wp_page_for_privacy_policy' 设置,仅在设置的页面生效。
- 在查询运行前调用会返回 false 并触发 _doing_it_wrong() 警告。
- 函数内部调用 WP_Query::is_privacy_policy() 方法。
代码示例
function is_privacy_policy() {
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_privacy_policy();
}注意事项
- 此函数在 WordPress 5.2.0 版本中引入。
- 相关函数包括 WP_Query::is_privacy_policy()、__() 和 _doing_it_wrong()。
- 常用于 get_body_class() 等函数中,以添加页面特定类名。
原文内容
Determines whether the query is for the Privacy Policy page.
Description
The Privacy Policy page is the page that shows the Privacy Policy content of the site.
is_privacy_policy() is dependent on the site’s “Change your Privacy Policy page” Privacy Settings ‘wp_page_for_privacy_policy’.
This function will return true only on the page you set as the “Privacy Policy page”.
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Source
function is_privacy_policy() {
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_privacy_policy();
}
Changelog
| Version | Description |
|---|---|
| 5.2.0 | Introduced. |