14

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?

1
  • 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 Commented Dec 1, 2011 at 10:54

3 Answers 3

31

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

7
  • This gives me a 500 internal server error Commented Dec 1, 2011 at 11:01
  • Are you sure it is because of the above code.. could I have a look at your author.php? Commented Dec 1, 2011 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 Commented Dec 1, 2011 at 11:07
  • Oops there is a typo.. its echo $author->ID... notice the '$'. Updated the answer. Commented Dec 1, 2011 at 11:08
  • 1
    Both of you missed a dollar sign. echo $author->ID; Commented Dec 1, 2011 at 11:09
11

Try this simply code

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

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 and acknowledge you have read our privacy policy.

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