get_the_author_meta()获取某个作者/用户的字段信息

目录

描述

This function returns a single meta data for a user. All meta data is available, but you must specify which field you want.

If used within The Loop, the user ID need not be specified, it defaults to current post author. A user ID must be specified if used outside The Loop.

get_the_author_meta() returns the data for use programmatically in PHP. To just display it instead, use the_author_meta()

If the specified meta field does not exist for this user, an empty string is returned.

get_the_author_meta() 描述

用法

 <?php get_the_author_meta$field$userID ); ?> 

get_the_author_meta() 用法

参数

$field 

(string) Field name for the data item to be returned. Valid values:

  • 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 (Biographical Info from the user's profile)
  • jabber
  • aim
  • yim
  • googleplus
  • twitter
  • user_level
  • user_firstname
  • user_lastname
  • rich_editing
  • comment_shortcuts
  • admin_color
  • plugins_per_page
  • plugins_last_view
  • ID

$userID 

(integer) If a user ID is passed to the function, it will return data for the specified user ID. Required if not in the loop. In the loop current author ID will be used.

get_the_author_meta() 参数

示例

Get A User's Email Address

Get the email address for the author of the current post and store it in the $user_email variable for further use. (Remember, this function returns data, it doesn't display it.)

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php $user_email = get_the_author_meta( 'user_email' ); ?>

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.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<p>Email the author: <a href="mailto:<?php echo get_the_author_meta( 'user_email', 25 ); ?>"><?php the_author_meta( 'display_name', 25 ); ?></a></p>

get_the_author_meta() 示例

注意

Plugins may add additional fields to the user profile, which in turn adds new key/value pairs to the wp_usermeta database table. This additional data can be retrieved by passing the field's key to the function as the $field parameter.

get_the_author_meta() 注意

历史

添加于 版本: 2.8.0

get_the_author_meta() 历史

源文件

get_the_author_meta() 函数的代码位于 wp-includes/author-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve the requested data of the author of the current post.
 * @link https://codex.wordpress.org/Template_Tags/the_author_meta
 * @since 2.8.0
 *
 * @global object $authordata The current author's DB object.
 *
 * @param string $field selects the field of the users record.
 * @param int $user_id Optional. User ID.
 * @return string The author's field from the current author's DB object.
 */
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' ) ) )
		$field = 'user_' . $field;

	$value = isset( $authordata->$field ) ? $authordata->$field : '';

	/**
	 * Filter 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|bool $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 );
}

get_the_author_meta() 源文件

相关