How to display user role on author page.

I have created my own role (group) so I want to display the user role on below the post and on author list.

I have tried this code but not working as its calling current_user and its showing current user role in all authors profile

<?php 
    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);

    if ($user_role == 'administrator') {
        echo 'Administrator';
    } elseif ($user_role == 'editor') {
        echo 'Editor';
    } elseif ($user_role == 'author') {
        echo 'Author';
    } elseif ($user_role == 'contributor') {
        echo 'Contributor';
    } elseif ($user_role == 'subscriber') {
        echo 'Subscriber';
    } else {
        echo '<strong>' . $user_role . '</strong>';
    }
?>

How can I alter this code to display user actual role and not the current user role.

2 Answers
2

Change:

$user_roles = $current_user->roles;

with

$user = new WP_User( $user_id );
$user_roles = $user->roles;

and the $user_id should e the actual user id who’s role you are trying to get.

Update,

Sorry i just read the author template part so try this:

//first get the current author whos page you are viewing
if(isset($_GET['author_name']))
        $curauth = get_user_by('slug', $author_name);
else
        $curauth = get_userdata(intval($author));
//then get the user object with roles
$user = new WP_User( $$curauth->ID );
$user_roles = $user->roles; 
....

Leave a Reply

Your email address will not be published. Required fields are marked *