Alexander
Alexander

Reputation: 45

Get the author name of current author archive

I am trying to get the author name of current author archive:

if (is_author()) return AUTHOR_NAME;

But how can I get current author name?

Upvotes: 2

Views: 1794

Answers (5)

kubi
kubi

Reputation: 965

No rewind_posts() tricks needed:

$qo = get_queried_object();
$author = $qo->data->display_name;

Upvotes: 1

user963714
user963714

Reputation:

global $wp_query;
$author = get_user_by('slug', $wp_query->query_vars['author_name']);

Upvotes: 2

dmtnexer
dmtnexer

Reputation: 27

This will display the author of the current page/post:

<?php echo get_the_author() ; ?>

Upvotes: -1

yelly
yelly

Reputation: 161

There is actually an interesting trick to this one. In an author archive, the one thing you know about all the posts in the query is that they are all by the author you want. In any case, the function you will want is get_the_author().

If you want the author name after you have already entered (and even exited) the loop, the last post that was loaded will be by the author, and using get_the_author() will be enough.

If, on the other hand, you want the author's name before you have entered the loop, you need to load the first post's data, and then rewind the query before the loop is entered as so:

if ( have_posts() ) {
    the_post();
    $author = get_the_author();
    rewind_posts();
}

If you don't need the loop at all, you can skip the rewinding.

If there are no posts in the query, you will have a slightly tougher job.

Upvotes: 2

Michal
Michal

Reputation: 3642

You can do it this way:

the_post();
$author_name = get_the_author();
rewind_posts();

Upvotes: 1

Related Questions