I want to display author role on archive and author page outside loop I found this code on stack overflow and it works fine within the loop
function get_author_role()
{
global $authordata;
$author_roles = $authordata->roles;
$author_role = array_shift($author_roles);
return $author_role;
}
In template using below code
echo get_author_role()
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?
$authordata
is not available before the first post is set up (before the firstthe_post();
).But all the data you need are already there on an author archive: in
get_queried_object()
.It is a
WP_User Object
on the author archive.Result:
So this will print the current authorâs first role: