Problem: I have a View of Users, and I would like to display the role of each User in the Loop.
Solution:
Add the following custom shortcode to your functions.php file:
function get_user_role_func( $atts ) { $a = shortcode_atts( array( 'userid' => '' ), $atts ); $userdata = get_user_by('ID', $a['userid']); $user_roles = $userdata->roles; $user_role = array_shift($user_roles); return $user_role; } add_shortcode( 'get_user_role', 'get_user_role_func' );
Then in your View, use the shortcode in a conditional like this:
[wpv-conditional if="( '[get_user_role userid='[wpv-user field='ID']']' eq 'editor' )"] I am Editor [/wpv-conditional] [wpv-conditional if="( '[get_user_role userid='[wpv-user field='ID']']' eq 'administrator' )"] I am Admin [/wpv-conditional]
Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user
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 |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 6 replies, has 2 voices.
Last updated by 6 years, 3 months ago.
Assisted by: Christian Cox.