SO网友:Md. Ehsanul Haque Kanan
尝试使用以下代码:
function themename_check_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) )
$user = get_userdata( $user_id );
else
$user = wp_get_current_user();
if ( empty( $user ) )
return false;
return in_array( $role, (array) $user->roles );
}
// example use for the current user
if ( themename_check_user_role( \'customer\' )
_e( "You\'ve got access!", \'themename\' );
else
_e( "Sorry, you don\'t have access!", \'themename\' );
这里,您要检查特定用户是否具有角色。如果找到匹配项,将返回true。