1
我正在使用我的第一个WP网站,并且需要在他们的帖子旁边显示作者的角色。像“Jimmy | Administrator”。看看作者的元数据可用:http://codex.wordpress.org/Function_Reference/the_author_meta不给我一种方式来访问。我确信有一个简单的方法可以做到这一点,我只是不知道它!谢谢!在Wordpress中获取作者的作用
我正在使用我的第一个WP网站,并且需要在他们的帖子旁边显示作者的角色。像“Jimmy | Administrator”。看看作者的元数据可用:http://codex.wordpress.org/Function_Reference/the_author_meta不给我一种方式来访问。我确信有一个简单的方法可以做到这一点,我只是不知道它!谢谢!在Wordpress中获取作者的作用
UPDATE:将这个在您的functions.php文件:
function get_author_role()
{
global $authordata;
$author_roles = $authordata->roles;
$author_role = array_shift($author_roles);
return $author_role;
}
那么你的WordPress的循环中调用此。所以:
<?php
if(have_posts()) : while(have_posts()) : the_post();
echo get_the_author().' | '.get_author_role();
endwhile;endif;
?>
... will print:'Jimmy |管理员'
完整答案:用户对象本身实际上存储角色和其他类型的有用信息。如果您想了解更多的通用功能的检索任何给定用户的角色,只需通过在你想要使用此功能,以针对用户的ID:
function get_user_role($id)
{
$user = new WP_User($id);
return array_shift($user->roles);
}
如果你想抓住的作者特定职位,叫它像这样:
<?php
if(have_posts()) : while(have_posts()) : the_post();
$aid = get_the_author_meta('ID');
echo get_the_author().' | '.get_user_role($aid);
endwhile;endif;
?>
响应最后的评论:
如果您需要获取WordPress的循环(我想象你想上的存档做外的数据和作者页面),您可以使用该功能从我的完整答案是这样的:
global $post;
$aid = $post->post_author;
echo get_the_author_meta('user_nicename', $aid).' | '.get_user_role($aid);
这将输出您想要在“用户|角色“格式
将会得到我当前用户的角色吗?我正在寻找作者的职位... – panzhuli
啊,我的道歉。将更新。 – maiorano84
TYVM :)。像一个魅力工作 – panzhuli