0

I have this code that will display all the roles for that particular user, but I only need to display 1 role from the user. I have installed bbpress so the code below will display out 2 roles.

$user = new WP_User( 1 );

if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role )
echo $role;                 
}

Result: Administrator Participant

How to display only the first role?

0

Try this:

if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
    $first_role = array_shift($user->roles);
    echo $first_role;   
}

Function array_shift obtains the first element of the $user->roles array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.