WordPress函数get_the_author_meta()获取当前帖子作者

WordPress函数get_the_author_meta()获取当前帖子作者

WordPress函数get_the_author_meta()获取当前帖子作者

  在 WordPress CMS内容管理系统中,get_the_author_meta()是一个内置函数,用于获取用户的元数据。元数据是一组自定义字段,可以为 WordPress 中的每个用户存储。

  推荐:[最新版]WordPress问答插件免费下载DW Question Answer Pro插件

get_the_author_meta()函数基本语法

描述

  检索当前帖子作者的请求数据

用法

get_the_author_meta( $field, $user_id );

参数

  $field

  (array可选)可选。要检索的用户字段。默认: ”。Default: ''

  $user_id

   (int|false) – 可选。用户身份。默认为当前帖子作者。默认值false

$field更多默认参数

admin_color
aim
comment_shortcuts
description
display_name
first_name
ID
jabber
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
yim

  推荐:8个最佳Chrome ChatGPT扩展程序

get_the_author_meta()函数

  get_the_author_meta()函数以字符串形式返回元字段的值。如果元字段不存在,则该函数将返回一个空字符串(源文件可参考这里

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

  推荐:NEX-Forms插件下载WordPress表单生成器插件+ Addons

如何使用get_the_author_meta()

  显示带换行符的作者简介,此示例将显示作者的简介(描述),同时保留换行符。

echo nl2br(get_the_author_meta('description'));

  获取作者ID,此示例检索循环内部和外部当前帖子的作者 ID。

// Outside loop
global $post;
$author_id = $post->post_author;

// Inside loop
$author_id = get_the_author_meta('ID');

  显示用户的显示名称及其链接的电子邮件地址,此示例将显示用户 ID 25 的电子邮件地址,并使用其显示名称作为锚文本来回显该地址。

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

  获取用户的电子邮件地址,此示例将获取当前帖子作者的电子邮件地址并将其存储在$user_email变量中以供进一步使用

$user_email = get_the_author_meta('user_email');

  显示带有换行符和换行段落的作者描述,此示例将显示作者的描述,同时保留换行符并将其段落包装到标签中。

echo wpautop(get_the_author_meta('description'));

  推荐:如何检查WordPress主题是否存在恶意代码

  推荐:WordPress函数使用手册

5/5 - (1 vote)

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部