the_author_meta()
云策文档标注
概述
the_author_meta() 是一个 WordPress 模板标签,用于输出用户数据库对象中的指定字段。默认情况下,它显示当前文章作者的信息,但也可通过参数指定其他用户。
关键要点
- 功能:输出用户元数据字段,如昵称、邮箱等,每次调用仅返回一个字段。
- 参数:$field(必需)指定要检索的字段,参考 get_the_author_meta() 获取可用字段列表;$user_id(可选)指定用户 ID,默认为 false,表示使用当前文章作者。
- 使用场景:在 The Loop 内使用时无需指定 $user_id;在 The Loop 外使用时需提供用户 ID。
- 注意事项:如果字段不存在,则不输出任何内容;若需返回值而非直接显示,应使用 get_the_author_meta()。
- 钩子:通过 apply_filters("the_author_{$field}", $author_meta, $user_id) 过滤输出值。
代码示例
// 显示用户 ID 25 的邮箱地址
the_author_meta('user_email', 25);注意事项
- 此函数直接输出内容,适用于模板显示;若需在代码中处理返回值,请使用 get_the_author_meta()。
- 字段名需准确,否则可能无输出;可参考相关函数获取支持的字段列表。
原文内容
Outputs the field from the user’s DB object. Defaults to current post’s author.
Description
See also
Parameters
$fieldstringrequired-
Selects the field of the users record. See get_the_author_meta() for the list of possible fields.
More Arguments from get_the_author_meta( … $field )
The user field to retrieve. Default empty.
$user_idint|falseoptional-
User ID. Defaults to the current post author.
Default:
false
Source
function the_author_meta( $field = '', $user_id = false ) {
$author_meta = get_the_author_meta( $field, $user_id );
/**
* Filters the value of the requested user metadata.
*
* The filter name is dynamic and depends on the $field parameter of the function.
*
* @since 2.8.0
*
* @param string $author_meta The value of the metadata.
* @param int|false $user_id The user ID.
*/
echo apply_filters( "the_author_{$field}", $author_meta, $user_id );
}
Hooks
- apply_filters( “the_author_{$field}”, string $author_meta, int|false $user_id )
-
Filters the value of the requested user metadata.
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
Skip to note 4 content
Codex
Display a User Email Address
Displays the email address for user ID 25.
<p></p>Skip to note 5 content
Codex
Advanced Uses
A plugin may add an additional field in the registration or manage users, which adds a new value in the
wp_usermeta(wherewp_is your data base prefix. For this example we will use a Twitter ID if a plugin set meta_key value to “twitter” and meta_value to “wordpress” then<p></p>would return:
This author’s Twitter name is WordPress
Skip to note 6 content
Peter Ph
And if you actually want to show the Author description using this function you can just do: