返回介绍

get_the_author_meta()

发布于 2017-09-11 00:30:33 字数 6568 浏览 1535 评论 0 收藏 0

get_the_author_meta( string $field = '',  int $user_id = false )

Retrieve the requested data of the author of the current post.


description


参数

$field

(string) (Optional) selects the field of the users record.

Default value: ''

$user_id

(int) (Optional) User ID.

Default value: false


返回值

(string) The author's field from the current author's DB object.


源代码

File: wp-includes/author-template.php

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 : '';

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

更新日志

Versiondescription
2.8.0Introduced.

More Information

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.

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.


相关函数

Uses

  • wp-includes/pluggable.php: get_userdata()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/author-template.php: get_the_author_{$field}

Used By

  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::column_author()
  • wp-admin/includes/class-wp-media-list-table.php: WP_Media_List_Table::column_author()
  • wp-includes/general-template.php: get_the_archive_description()
  • wp-admin/includes/export.php: export_wp()
  • wp-admin/includes/revision.php: wp_prepare_revisions_for_js()
  • wp-includes/general-template.php: feed_links_extra()
  • wp-includes/deprecated.php: get_profile()
  • wp-includes/deprecated.php: get_the_author_icq()
  • wp-includes/deprecated.php: get_the_author_yim()
  • wp-includes/deprecated.php: get_the_author_msn()
  • wp-includes/deprecated.php: get_the_author_aim()
  • wp-includes/deprecated.php: get_author_name()
  • wp-includes/deprecated.php: get_the_author_url()
  • wp-includes/deprecated.php: get_the_author_ID()
  • wp-includes/deprecated.php: get_the_author_description()
  • wp-includes/deprecated.php: get_the_author_login()
  • wp-includes/deprecated.php: get_the_author_firstname()
  • wp-includes/deprecated.php: get_the_author_lastname()
  • wp-includes/deprecated.php: get_the_author_nickname()
  • wp-includes/deprecated.php: get_the_author_email()
  • wp-includes/post-template.php: wp_post_revision_title_expanded()
  • wp-includes/author-template.php: the_author_meta()
  • wp-includes/author-template.php: get_the_author_link()
  • Show 18 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 5You must log in to vote on the helpfulness of this note Contributed by studio-jt

    Display the author bio (description) keeping the line break

    <?php echo nl2br(get_the_author_meta('description')); ?>
  2. 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 ); ?>"><?php
    	the_author_meta( 'display_name', 25 ); ?></a></p>
    
    

    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.)

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

    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 https://developer.wordpress.org/reference/functions/wpautop/
     */
    echo wpautop( get_the_author_meta( 'description' ) );
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文