If/Else语句-不适用于管理员角色-wordpress


If / Else statement - not working for admin role - wordpress

我的Wordpress站点中有4个不同的用户角色+额外的管理员角色。

我有一个<div>,我只想显示给"经理"用户角色。

以下代码应仅针对"经理"用户角色。因此,如果用户具有"经理"用户角色,他们将在<p>块中看到这样的通知:这是一个高级功能。您必须具备上传横幅的高级功能

Else如果用户"不是"经理",他们将可以选择上传横幅。

我有这个代码:

<?
if(current_user_can('manager')) { ?>    
    <div class="button-area">
        <i class="fa fa-cloud-upload"></i>
        <p class="banner-help-block-heading">UPLOAD BANNER</p>
        <p class="banner-help-block">This is a premium feature. You must have premium capabilities to upload your banner</p>
        <div class="upgrade-button">
            <a href="#" class="button-green-upgrade">Upgrade Account</a>
        </div>
    </div><?
        $manager = true;
    }
else { ?>
    <div class="button-area<?php echo ($banner)? ' elect-hide':''; ?>">
        <i class="fa fa-cloud-upload"></i>
        <a href="#" class="elect-banner-drag elect-btn elect-btn-info"><?php _e('Upload banner', 'elect'); ?></a>
        <p class="help-block"><?php _e('Upload a banner for your profile. Banner size is (825x300) pixel.', 'elect'); ?></p>
    </div><?php 
    $manager = false;
    } ?>
            </div>  

它适用于包括"经理"在内的所有用户角色,但如果我将页面视为"管理员",我就会看到"经理"应该看到的内容。我看到了高级功能通知。但相反,我应该能够上传一个横幅。

那么我做错了什么?

如果要检查当前登录用户的用户角色,则必须检查current_user-object上的角色。

$user_roles = $current_user->roles; // Array

但检查用户能力总是更好的。

如果你仍然想检查用户的角色,这里有一个我写的函数。。。也许它能帮助你

function mi_checkUserRole($role, $userid = null) {
    $user = null;
    if ( !empty($userid) ) {
        $user = new WP_User( $userid );
    } else {
        global $current_user;
        get_currentuserinfo();
        $user = $current_user;
    }
    if ( !empty($user) ) {
        return in_array($role, $user->roles);
    }
    return false;
}
// Example:
$isManager = mi_checkUserRole("manager");