在WordPress中的作者存档页面上获取作者姓名

在WordPress中的作者存档页面上获取作者姓名

问题描述:

I am making template for archives in WordPress website, but my template will not work for author name on author archive.

I have this:

<?php if (is_category('')) { ?>
<?php single_cat_title(); ?>

<?php } elseif (is_author('')) { ?>
<?php get_userdata( get_query_var('author') );?>

<?php } elseif (is_day('')) { ?>
<?php echo get_the_time('F j, Y'); ?>

<?php } elseif (is_month('')) { ?>
<?php echo get_the_time('F Y'); ?>

<?php } elseif (is_year('')) { ?>
<?php echo get_the_time('Y'); ?>

<?php } elseif (is_tag('')) { ?>
<?php echo single_tag_title(''); ?>
<?php } ?>

This part will not work:

<?php } elseif (is_author('')) { ?>
<?php get_userdata( get_query_var('author') );?>

我正在为WordPress网站制作档案模板,但我的模板不适用于作者档案的作者姓名。

我有这个: p>

 &lt;?php if(is_category('')){?&gt; 
&lt;?  php single_cat_title();  ?&gt; 
 
&lt;?php} elseif(is_author('')){?&gt; 
&lt;?php get_userdata(get_query_var('author'));?&gt; 
 
&lt;?php} elseif  (is_day('')){?&gt; 
&lt;?php echo get_the_time('F j,Y');  ?&gt; 
 
&lt;?php} elseif(is_month('')){?&gt; 
&lt;?php echo get_the_time('F Y');  ?&gt; 
 
&lt;?php} elseif(is_year('')){?&gt; 
&lt;?php echo get_the_time('Y');  ?&gt; 
 
&lt;?php} elseif(is_tag('')){?&gt; 
&lt;?php echo single_tag_title('');  ?&gt; 
&lt;?php}?&gt; 
  code>  pre> 
 
 

此部分不起作用: p>

 &lt  ;?php} elseif(is_author('')){?&gt; 
&lt;?php get_userdata(get_query_var('author'));?&gt; 
  code>  pre> 
  div>

The function you're using in is_author returns a user object. It doesn't output anything.

Correct usage would be:

<?php 
// Get current author.
$curauth = ( get_query_var( 'author_name' ) ) ? get_user_by( 'slug', get_query_var( 'author_name' ) ) : get_userdata( get_query_var( 'author' ) ); ?>

Author: <?php echo $curauth->nickname; ?>

Also the empty string isn't necessary with is_category etc.

WordPress supports an archive template for that. You simply have to add an author.php file to your theme folder: http://codex.wordpress.org/Author_Templates

And than you can use all author functions like:

  • get_the_author() for the author name
  • get_the_author_meta('description') for the description
  • get_avatar( get_the_author_meta('ID'), 40 ) for the avatar
  • ...

There are also archives for categories (http://codex.wordpress.org/Category_Templates) and Tags (http://codex.wordpress.org/Tag_Templates) btw.