10

This is probably a super simple question.

But how do I echo the ID of a user on author.php?

I've tried

the_author_meta('ID')

But it didn't seem to want to work. I want to echo it at the end of a URL, for example;

http:///www.domain.com/author/sampleauthor-id

Obviously, where "id" is that particular author's ID

Any ideas?

  • Not the permalinks, its for a modified version of the UserPhoto plugin. I want to hardcode a particular image link into author.php and as each user's images are appeneded with their author ID I need to include that in the link – Dean Elliott Dec 1 '11 at 10:54
27

Try this code.

$author = get_user_by( 'slug', get_query_var( 'author_name' ) );
echo $author->ID;

Alternatively, if author name has not been set use:

if ( $author_id = get_query_var( 'author' ) ) { $author = get_user_by( 'id', $author_id ); }

credit @AndyAdams in the easily missed comments bellow

  • This gives me a 500 internal server error – Dean Elliott Dec 1 '11 at 11:01
  • Are you sure it is because of the above code.. could I have a look at your author.php? – Joshua Abenazer Dec 1 '11 at 11:05
  • $author = get_user_by( 'slug', get_query_var( 'author_name' ) ); gave no problems, it was the echo author->ID; that caused the error for some reason – Dean Elliott Dec 1 '11 at 11:07
  • Oops there is a typo.. its echo $author->ID... notice the '$'. Updated the answer. – Joshua Abenazer Dec 1 '11 at 11:08
  • 1
    Both of you missed a dollar sign. echo $author->ID; – Johannes Pille Dec 1 '11 at 11:09
3

Try this simply code

if (is_author()){
    $author = get_queried_object();
    $author_id = $author->ID;
}
  • A simpler way to do this would be $author_id = get_queried_object_id(); – Gavin Nov 17 '17 at 20:20
0

if var_dump(the_author_meta('ID')) is Null your author has no posts.

Try use echo $author if you call it inside author.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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