Question

I want to display author role on archive and author page outside loop I found this code and it works fine within the loop Getting an author's role in Wordpress

But when I am adding this to archive and author page its giving me warning message called

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

How to solve this?

Was it helpful?

Solution

These examples are using the function I gave in my COMPLETE ANSWER Here. In your functions.php file:

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

In your archive page:

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;

As for your author template, the Wordpress Codex on Author Templates has a lot of useful information. You can do something like this:

<?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);
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top