2012-05-07 37 views

回答

12

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); 

这将输出您想要在“用户|角色“格式

+0

将会得到我当前用户的角色吗?我正在寻找作者的职位... – panzhuli

+0

啊,我的道歉。将更新。 – maiorano84

+0

TYVM :)。像一个魅力工作 – panzhuli