WordPress获取文章作者信息函数get_the_author_meta()

get_the_author_meta() 函数用来获取网站用户的信息,如果在循环中则不需要指定用户的 ID,直接调用当前文章的作者的信息,否则必须得指定需要获取的用户的 ID

用法

get_the_author_meta( $field, $user_id );

参数

$field

(字符串)(可选)要返回的数据项,可选值:

  • user_login
  • user_pass
  • user_nicename
  • user_email
  • user_url
  • user_registered
  • user_activation_key
  • user_status
  • roles
  • display_name
  • nickname
  • first_name
  • last_name
  • description
  • jabber
  • aim
  • yim
  • user_level
  • user_firstname
  • user_lastname
  • rich_editing
  • comment_shortcuts
  • admin_color
  • plugins_per_page
  • plugins_last_view
  • ID

默认值:None

$user_id

(整数)(可选)需要返回的数据的用户 ID。如果在循环里可以不指定,会自动指定为当前文章的作者。

默认值:None

例子

获取用户邮箱:

$user_email = get_the_author_meta( 'user_email' );

获取指定 ID 用户的显示名称:

get_the_author_meta( 'display_name', 25 );

在主循环外获取当前文章作者

global $post;
$author_id=$post->post_author;
//获取用户昵称
$nickname = get_the_author_meta( 'nickname',  $author_id);

其它

该函数位于:wp-includes/author-template.php

还有一个 the_author_meta() 函数,和 get_the_author_meta() 的用法完全一样,只是一个是返回数据,一个是直接打印数据。