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

use*_*424 6 php wordpress

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

我有这个:

<?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 } ?>
Run Code Online (Sandbox Code Playgroud)

这部分将不起作用:

<?php } elseif (is_author('')) { ?>
<?php get_userdata( get_query_var('author') );?>
Run Code Online (Sandbox Code Playgroud)

Nat*_*son 1

您在 is_author 中使用的函数返回一个用户对象。它不输出任何内容。

正确的用法是:

<?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; ?>
Run Code Online (Sandbox Code Playgroud)

is_category 等也不需要空字符串。