在 WordPress 中使用 PHP 调用作者的用户名

问题描述 投票:0回答:1

我需要调用帖子作者的用户名。 到目前为止,我已经尝试过:

$post_id =  get_the_ID();
$author_id = get_post_field ('post_author', $post_id);
$display_name = get_the_author_meta( 'nickname' , $author_id );

此代码进入后循环并调用昵称,然后使用

$display_name
调用信息。

它可以工作,但不能满足我的需要,它只调用昵称。我尝试将“昵称”更改为“用户名”:

$post_id =  get_the_ID();
$author_id = get_post_field ('post_author', $post_id);
$display_name = get_the_author_meta( 'username' , $author_id );

但是没有成功。

php wordpress user-data usermetadata
1个回答
0
投票

要获取作者用户名(或显示名称),请尝试以下操作:

$author_id = get_post_field ('post_author', get_the_ID()); 

if ( $author_id > 0 ) {
    $author = get_userdata( $author_id );
    $user_name    = $author->user_login;
    $display_name = $author->display_name;
}
© www.soinside.com 2019 - 2024. All rights reserved.