admin管理员组文章数量:1391929
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.
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.
Share Improve this question edited Mar 5, 2013 at 22:26 powtac 1234 bronze badges asked Feb 3, 2012 at 13:21 honk31honk31 1,4081 gold badge13 silver badges23 bronze badges5 Answers
Reset to default 2To 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;
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.
本文标签: 404 errorShow author archive pages for authors with no posts
版权声明:本文标题:404 error - Show author archive pages for authors with no posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744704749a2620774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论