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