2012-05-07 54 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