1

I need to call in the username of a post's author.

I have tried WordPress: get author info from post id answer code.

I have added the code in the post loop, echoing $display_name.

It works, but not for what I need, as it displays the nickname.

So I tried changing the 'nickname' to 'username' like:

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

But it didn't work.

0

1 Answer 1

0

The field key for the username in WordPress is user_login… You could also use the display name which field key is display_name.

To get the author username (or display name) try the following instead:

$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;
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.