获取当前作者档案的作者姓名

时间:2013-09-27 21:25:52

标签: wordpress

我想获取当前作者档案的作者姓名:

if (is_author()) return AUTHOR_NAME;

但我怎样才能获得当前的作者姓名?

5 个答案:

答案 0 :(得分:1)

你可以这样做:

the_post();
$author_name = get_the_author();
rewind_posts();

答案 1 :(得分:1)

这个实际上有一个有趣的技巧。 在作者档案中,您对查询中所有帖子的了解就是它们都是您想要的作者。在任何情况下,您需要的功能是get_the_author()

如果您想在输入(甚至退出)循环后想要作者姓名,那么加载的最后一篇文章将由作者完成,使用get_the_author()就足够了。

另一方面,如果在进入循环之前需要作者姓名,则需要加载第一个帖子的数据,然后在输入循环之前回退查询:

if ( have_posts() ) {
    the_post();
    $author = get_the_author();
    rewind_posts();
}

如果您根本不需要循环,则可以跳过倒带。

如果查询中没有帖子,您的工作会稍微艰苦一些。

答案 2 :(得分:1)

global $wp_query;
$author = get_user_by('slug', $wp_query->query_vars['author_name']);

答案 3 :(得分:0)

这将显示当前页面/帖子的作者:

<?php echo get_the_author() ; ?>

答案 4 :(得分:0)

不需要rewind_posts()个技巧:

$qo = get_queried_object();
$author = $qo->data->display_name;