Conditional tag based on the role of author in author.php

current_user_can does not give the desired output when working with roles. There is a trac ticket #22624 explaining this all. It was closed with the following

  • Keywords close removed

  • Milestone Awaiting Review deleted

  • Resolution set to wontfix

  • Status changed from new to closed

My solution would be to get the role of the featured user and then check that against the specific roles

The first thing would be to retrieve the author’s role with get_queried_object()->roles and then check of the desired role is in array, and if it is, do something. Here is the code

$user_role = get_queried_object()->roles[0];

if( in_array( strtolower('Editor'), $user_role ) ) {
    //Do something for Editor
}elseif( in_array( strtolower('Contributor'), $user_role ) ) { 
    //Do something for Contributor
}

EDIT

User roles starts with uppercase letters, but the roles returned from get_queried_object()->roles starts with lowercase letters. That is why I’ve use the roles as such in my code and used strtolower to convert the roles to lowercase