0

I am using the admin role and 2 custom roles. On the author.php i want to be able to place the role of that user.

I am using this to pull the role out in text

$curauth = $wp_query->get_queried_object();
$user_id = $curauth->ID;
$user = new WP_User( $user_id );
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
    foreach ( $user->roles as $role )
        echo $role;
}

Put i want to be able to put that into something like

if (current_user_can('buyer')) { 
        get_template_part( 'buyer', 'edit' ); } 
      else if (current_user_can('courier')) { 
        get_template_part( 'courier', 'edit' ); } 
      else if (current_user_can('banned')) { 
        get_template_part( 'account', 'banned' ); }
      else if (is_user_logged_in()) { 
        get_template_part( 'account', 'error' );
    } else {  }

Which using for the current users, i need to make that work for the author user.

3
  • You question is a little confusing. The first code echoes a user's role. The second code adds a template part based on their role. What, exactly, are you trying to do? Commented Apr 12, 2013 at 21:05
  • The second code echos the current user, i want it to echo the author so i may use it in the author.php. Commented Apr 12, 2013 at 22:14
  • If my answer worked please mark it as the solution. Commented Apr 16, 2013 at 17:46

1 Answer 1

1

As an alternative to "current_user_can" WordPress provides "user_can" which will take a User object or ID as a parameter. Combining your top code and the bottom code with the new function it would look like this:

$author_user = $wp_query->get_queried_object(); // the queried object in 'author.php' should be an author user

if (user_can($author_user, 'buyer')) { 
  get_template_part( 'buyer', 'edit' );
} else if (user_can($author_user, 'courier')) { 
  get_template_part( 'courier', 'edit' );
} else if (user_can($author_user, 'banned')) { 
  get_template_part( 'account', 'banned' ); 
} else {
  // etc...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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