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