2012-05-29 49 views
1

我要显示的存档和作者网页外循环笔者作用,我发现这个代码,并在循环中正常工作 Getting an author's role in Wordpress显示作者的角色

但是,当我加入这个归档和作者页面给我的警告信息叫做

Warning: array_shift() expects parameter 1 to be array, null given in 

如何解决这个问题?

回答

2

这些示例使用我在我的完整答案Here中给出的函数。在您的functions.php文件:

function get_user_role($id) 
{ 
    $user = new WP_User($id); 
    return array_shift($user->roles); 
} 

在你的档案页:

if(have_posts()) : while(have_posts()) : the_post(); 
    $aid = $post->post_author; 
    echo get_the_author_meta('user_nicename', $aid).' | '.get_user_role($aid); 
endwhile;endif; 

至于你的作者模板时,Wordpress Codex on Author Templates有很多有用的信息。你可以这样做:

<?php 
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author')); 
echo $curauth->user_nicename.' | '.get_user_role($curauth->ID); 
?> 
+0

酷!我测试过不同的模板,我相信它的工作正常。让我检查我的模板,并回到你身边。非常感谢 –

+0

这工作完全正常。非常感谢。 –