get_the_author_meta()
云策文档标注
概述
get_the_author_meta() 函数用于检索当前文章作者的用户数据,支持多种字段参数,适用于程序化处理。在循环内使用时默认基于当前作者,循环外需指定用户ID。
关键要点
- 函数返回作者字段的字符串值,若字段不存在则返回空字符串。
- 参数 $field 可选,支持预定义字段如 admin_color、display_name、user_email 等,也支持插件添加的自定义用户元数据。
- 参数 $user_id 可选,默认为 false,在循环内自动使用当前作者ID,循环外必须指定。
- 与 the_author_meta() 区别:get_the_author_meta() 用于获取数据,the_author_meta() 用于直接输出。
- 函数内部使用动态过滤器 apply_filters( "get_the_author_{$field}", $value, $user_id, $original_user_id ) 允许对返回值进行过滤。
代码示例
// 获取作者ID(循环内)
$author_id = get_the_author_meta('ID');
// 获取作者邮箱(指定用户ID)
$email = get_the_author_meta('user_email', 25);
// 使用 wpautop 格式化作者描述
echo wpautop(get_the_author_meta('description'));注意事项
- 在循环外使用时,必须通过全局 $post 或类似方式获取用户ID,例如:global $post; $author_id = $post->post_author;
- 字段名如 'login' 会自动转换为 'user_login',但建议直接使用完整字段名以避免混淆。
- 插件添加的用户元数据可通过字段键直接检索,扩展了函数功能。
原文内容
Retrieves the requested data of the author of the current post.
Description
Valid values for the $field parameter include:
- admin_color
- comment_shortcuts
- description
- display_name
- first_name
- ID
- last_name
- nickname
- plugins_last_view
- plugins_per_page
- rich_editing
- syntax_highlighting
- user_activation_key
- user_description
- user_email
- user_firstname
- user_lastname
- user_level
- user_login
- user_nicename
- user_pass
- user_registered
- user_status
- user_url
Parameters
$fieldstringoptional-
The user field to retrieve. Default empty.
$user_idint|falseoptional-
User ID. Defaults to the current post author.
Default:
false
Source
function get_the_author_meta( $field = '', $user_id = false ) {
$original_user_id = $user_id;
if ( ! $user_id ) {
global $authordata;
$user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
} else {
$authordata = get_userdata( $user_id );
}
if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ), true ) ) {
$field = 'user_' . $field;
}
$value = isset( $authordata->$field ) ? $authordata->$field : '';
/**
* 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
* @since 4.3.0 The `$original_user_id` parameter was added.
*
* @param string $value The value of the metadata.
* @param int $user_id The user ID for the value.
* @param int|false $original_user_id The original user ID, as passed to the function.
*/
return apply_filters( "get_the_author_{$field}", $value, $user_id, $original_user_id );
}
Hooks
- apply_filters( “get_the_author_{$field}”, string $value, int $user_id, int|false $original_user_id )
-
Filters the value of the requested user metadata.
Skip to note 8 content
aThemeArt
Get the author ID Outside loop:
global $post; $author_id = $post->post_author;Get the author ID inside a loop :
$author_id = get_the_author_meta( 'ID' );bellow is a few examples of author value :
// to get nicename get_the_author_meta( 'nicename', $author_id ); // to get email get_the_author_meta( 'email', $author_id ); // to get url get_the_author_meta( 'url', $author_id ); // to get status get_the_author_meta( 'status', $author_id );Skip to note 9 content
studio-jt
Display the author bio (description) keeping the line break
Skip to note 10 content
Milana Cap
Using the wpautop() for description will also keep the line break (like in studio-jt’ comment) but will output cleaner html:
/** * Display Author's description * with keeping line breaks and wrapping its paragraphs * into <p> tag * * @link <a href="https://developer.wordpress.org/reference/functions/wpautop/" rel="ugc">https://developer.wordpress.org/reference/functions/wpautop/</a> */ echo wpautop( get_the_author_meta( 'description' ) );Skip to note 11 content
Salim
Get the author ID:
Skip to note 12 content
Codex
Show a User’s Display Name With Email Address Linked
Get the email address for user ID 25, and echo it using their display name as the anchor text.
<p>Email the author: <a href="mailto:<?php echo get_the_author_meta( 'user_email', 25 ); ?>"> </a> </p>Skip to note 13 content
Codex
Get A User’s Email Address
Get the email address for the author of the current post and store it in the
$user_emailvariable for further use. (Remember, this function returns data, it doesn’t display it.)Skip to note 14 content
karlazz
What are the author meta that I can use in “field”?
Could they be added to this page? Or a link to another page that lists them could be added perhaps?
add_user_meta()