is_author()
云策文档标注
概述
is_author() 是 WordPress 中的一个条件标签函数,用于判断当前查询是否为现有作者存档页面。它可接受可选参数来检查特定作者。
关键要点
- 函数返回布尔值,指示查询是否针对现有作者存档页面。
- 可选参数 $author 可以是用户 ID、昵称、nicename 或其数组,用于匹配特定作者。
- 在查询运行前调用此函数会触发 _doing_it_wrong() 并返回 false。
- 函数内部调用 WP_Query::is_author() 实现核心逻辑。
代码示例
is_author();
// 当显示任何作者页面时。
is_author('4');
// 当显示用户 ID 为 4 的作者存档页面时。
is_author('Vivian');
// 当显示昵称为 "Vivian" 的作者存档页面时。
is_author('john-jones');
// 当显示 nicename 为 "john-jones" 的作者存档页面时。
is_author(array(4,'john-jones','Vivian'));
// 当显示作者为用户 ID 4、nicename "john-jones" 或昵称 "Vivian" 的存档页面时。
// 注意:数组功能在版本 2.5 中添加。
原文内容
Determines whether the query is for an existing author archive page.
Description
If the $author parameter is specified, this function will additionally check if the query is for one of the authors specified.
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Parameters
$authorint|string|int[]|string[]optional-
User ID, nickname, nicename, or array of such to check against. Default empty.
Source
function is_author( $author = '' ) {
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_author( $author );
}
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
Skip to note 2 content
Codex
Examples
is_author(); // When any Author page is being displayed. is_author('4'); // When the archive page for Author number (ID) 4 is being displayed. is_author('Vivian'); // When the archive page for the Author with Nickname "Vivian" is being displayed. is_author('john-jones'); // When the archive page for the Author with Nicename "john-jones" is being displayed. is_author(array(4,'john-jones','Vivian')); // When the archive page for the author is either user ID 4, or user_nicename "john-jones", // or nickname "Vivian". Note: the array ability was added at Version 2.5.