WordPress函数:get_the_author_meta 获取作者的属性

江河/ 2023年05月01日/ WordPress/ 浏览 680

函数原型:


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


获取作者的属性。


参数说明:


$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


$user_id,如果在The Loop中使用,则无需指定用户ID,默认为当前文章作者。如果在循环之外使用,则必须指定用户ID。


get_the_author_meta()返回数据,以便在PHP中以编程方式使用。要只显示它,请使用_author_meta()


如果此用户不存在指定的元字段,则返回一个空字符串。


插件可以向用户配置文件添加额外的字段,从而向wp_usermeta数据库表添加新的键/值对。可以通过将字段的键作为$field参数传递给函数来检索这些附加数据。


函数源码:


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

	return apply_filters( "get_the_author_{$field}", $value, $user_id, $original_user_id );
}


包含钩子:


apply_filters( "get_the_author_{$field}", string $value, int $user_id, int|false $original_user_id )


使用举例:


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


发表评论

暂无评论,抢个沙发...

客服 工单