Question

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.

Was it helpful?

Solution

To access the author user object outside of the loop on an author archive, you can do the following:

global $curauth;
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
echo 'This is the author page of '.$curauth->display_name;

OTHER TIPS

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:

<?php if ( have_posts() )  : ?>

It basically says: if the authors has published some posts, then... Well, I just added: "OR NOT!"

<?php if ( have_posts() || !have_posts() )  : ?>

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:

echo get_avatar( get_the_author_meta( 'ID' ), 80 );

I use:

get_avatar( $curauth->ID, 80 );

You have to set the $curauth variable properly first thing after the get_header function, though.

get_header(); 
$curauth = (get_query_var('author_name')) 
    ? get_user_by('slug', get_query_var('author_name')) 
    : get_userdata(get_query_var('author'));

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:

function slug_show_authors_without_posts( $template ) {
    global $wp_query;

    if ( $wp_query->query_vars[ 'post_type' ] === 'author' ) {

        return get_author_template();
    }

    return $template;
}
add_filter( '404_template', 'slug_show_authors_without_posts' );

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 user foo for URL /author/foo/) no matter if there are any posts for the given user.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top