I have one page for each author, but if the author doesn’t have any posts I can’t get its variables, because the loop is empty.
The page returns empty, no avatar, no info and stuff, the whole site is empty.
How do I get WordPress to pull out the author variable if there are no posts (what makes no author, but i am on the authors page)..?
I’m using twentyeleven as parent theme.
To access the author user object outside of the loop on an author archive, you can do the following:
I cracked it!
In my case I first created an author.php page (my theme lacked one) from the archive.php by cloning and cleaning it.
Then I found this piece of code:
It basically says: if the authors has published some posts, then…
Well, I just added: “OR NOT!”
And it works beautifully (see here)! The “OR NOT” is “
||
= OR” and “!
= NOT”.Also, I had to change the way the template was retrieving the Author’s avatar, because
get_avatar
does not work correctly if the Author/User hasn’t published anything yet, or if his/her posts are all still “Private” and not “Public”.I studied the Codex and came upon the following solution. Instead of:
I use:
You have to set the
$curauth
variable properly first thing after theget_header
function, though.I once wrote a WordPress Plugin called
Show authors without posts
feel free to use it…
You can redirect the 404 back to the author template with this function:
Here are 2 important things to keep in mind when using this solution:
1) This breaks the template hierarchy’s fallback system. So if your theme doesn’t have an author.php, you will have a new problem since it will not fallback to index.php like you’re used to.
TL;DR You must have a author.php in your theme.
2) This doesn’t address the issue that most author.php templates test if the author has posts and if not, returns no post warning. You will need to modify or remove the loop in your author.php to prevent this.
TL;DR Your author.php is probably designed to make this not work. You need to fix this.
get_query_var('author')
will give you the user ID of the opened page (e.g. the ID of userfoo
for URL/author/foo/
) no matter if there are any posts for the given user.