Skip Navigation

[Resolved] How to display user role in loop?

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to display multiple user role in loop conditional output?

Solution:
1. Add this code in your theme’s or child theme’s functions.php file:

function get_usersrole( $atts ) {
  
    // Attributes
    $atts = shortcode_atts(
        array(
            'user_id' => '',
        ),
        $atts
    );
  
    $user_info = get_userdata($atts['user_id']);
          
          return implode(', ', $user_info->roles);
         
}
add_shortcode( 'get_usersrole', 'get_usersrole' );

2. Register the ‘get_usersrole’ shortcode first in Toolset >> Settings >> Front-end Content >> Third-party shortcode arguments.

3. Then add this shortcode in your View >> Loop Output Editor or Content template:

[wpv-conditional if="( '[get_usersrole user_id='[wpv-user field='ID']']' eq 'administrator' ) OR ( '[get_usersrole user_id='[wpv-user field='ID']']' eq 'contributor' )"]
 
<strong>[wpv-user field="nickname"]</strong>
 
[/wpv-conditional]
This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by romanB-3 7 years, 3 months ago.

Assisted by: Noman.

Author
Posts
#558048

Hello,
I've read this post https://toolset.com/forums/topic/how-to-display-user-role-in-loop/ and am in exact same need.
This is what I get :

Notice: Trying to get property of non-object in /home/netispgaja/adage/wp-content/themes/toolset-starter-child/functions.php on line 541

Warning: implode(): Invalid arguments passed in /home/netispgaja/adage/wp-content/themes/toolset-starter-child/functions.php on line 541

Notice: Trying to get property of non-object in /home/netispgaja/adage/wp-content/themes/toolset-starter-child/functions.php on line 541

Warning: implode(): Invalid arguments passed in /home/netispgaja/adage/wp-content/themes/toolset-starter-child/functions.php on line 541

Notice: Trying to get property of non-object in /home/netispgaja/adage/wp-content/themes/toolset-starter-child/functions.php on line 541

Warning: implode(): Invalid arguments passed in /home/netispgaja/adage/wp-content/themes/toolset-starter-child/functions.php on line 541

Notice: Trying to get property of non-object in /home/netispgaja/adage/wp-content/themes/toolset-starter-child/functions.php on line 541

Warning: implode(): Invalid arguments passed in /home/netispgaja/adage/wp-content/themes/toolset-starter-child/functions.php on line 541

Notice: Trying to get property of non-object in /home/netispgaja/adage/wp-content/themes/toolset-starter-child/functions.php on line 541

Warning: implode(): Invalid arguments passed in /home/netispgaja/adage/wp-content/themes/toolset-starter-child/functions.php on line 541

Notice: Trying to get property of non-object in /home/netispgaja/adage/wp-content/themes/toolset-starter-child/functions.php on line 541

Warning: implode(): Invalid arguments passed in /home/netispgaja/adage/wp-content/themes/toolset-starter-child/functions.php on line 541

It seems to me it's because the code doesn't get the user ID, but I can't see where to get it ?

Thank you.

#558092

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Roman,

Thank you for contacting Toolset support. The code is working at my end, I have just replaced getuser_role to get_usersrole in the code, like this:

function get_usersrole( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'user_id' => '',
        ),
        $atts
    );
 
    $user_info = get_userdata($atts['user_id']);
         
          return implode(', ', $user_info->roles);
        
}
add_shortcode( 'get_usersrole', 'get_usersrole' );

Then use the shortcode:

[get_usersrole user_id='[wpv-user field="ID"]'] 

Thank you

#558114

Great. That works.
Thank you.

#558278

I'm sorry I'll reopen this ticket because I can't get this work for various user roles at a time.

I have this conditional output

[wpv-conditional if="( '[getuser_role user_id='[wpv-user field='ID']']' eq 'respachats' )"]

and wonder how it could work for various user roles.

I tried to separate them by comas

[wpv-conditional if="( '[getuser_role user_id='[wpv-user field='ID']']' eq 'respachats,direction' )"]

but of course it didn't work. Is it possible ?

Thank you.

#558290

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

user role.png

Hello Roman,

I can see you are still using getuser_role instead of get_usersrole. Please make sure to use the right shortcode, that is get_usersrole.

1. To use the custom shortcode in conditional group, you first need to register this ‘get_usersrole’ shortcode in Toolset >> Settings >> Front-end Content >> Third-party shortcode arguments And Functions inside conditional evaluations. See attached screenshot.

2. Then use the shortcode in the view like this:

[wpv-conditional if="( '[get_usersrole user_id='[wpv-user field='ID']']' eq 'administrator' ) OR ( '[get_usersrole user_id='[wpv-user field='ID']']' eq 'contributor' )"]

<strong>[wpv-user field="nickname"]</strong>

[/wpv-conditional]

Thank you

#558556

Thank you very much.