Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Justin Estrada
Justin Estrada
34,995 Points

is_author of this profile page of a certain user role. for example is /author/visitor a certain user role phpconditional

Searched through the codex for just a little thought I'd ask treehouse community.

How would you go about finding the author role of a users page under /author. (I'm not trying to find the logged in persons role, but the author page a visitor is viewing)

Hope that makes sense!

Jacobus Hindson
Jacobus Hindson
14,429 Points

Look at using a combination of get_the_author and $user->roles to echo out the author role...would probably only work if logged in/higher role level - but its a possibility.

1 Answer

Justin Estrada
Justin Estrada
34,995 Points

Hello everyone here is the code I used, to accomplish what I needed!

I wanted to get the author of the /author/user page I wanted so I used the wordpress function get_user_by. Then I used the function user_can to get the user roles with the user id parameter and their capabilities.

The if/elseif statement below says if the user is an admin say adminsupport with like a font awesome icon I used, the second elseif says if they're of user role author say Instructor with font awesome icon and last is if they are a contributor say Member with a check font awesome icon

<?php $author = get_user_by( 'slug', get_query_var( 'author_name' ) ); ?> <?php if(user_can( $author->ID, 'remove_users' )): ?><h2><i class="fa fa-life-saver"></i><?php _e('Admin/Support'); ?></h2> <?php elseif(user_can( $author->ID, 'delete_published_posts' )): ?><h2><i class="fa fa-mortar-board"></i><?php _e('Instructor'); ?></h2> <?php elseif(user_can( $author->ID, 'edit_posts' )): ?><h2><i class="fa fa-check-square-o"></i><?php _e('Member'); ?></h2><?php endif; ?>

Hope this helps anyone who stumbles upon this. Cheers