根据用户角色/能力从菜单中隐藏 DIV Wordpress

Hiding DIV from menu based on user role / capabilities Wordpress

我有一个 wordpress 网站,有 2 个用户角色,客户和供应商。我想从客户的菜单栏中隐藏供应商仪表板。该主题的代码位于 header-aside.php 文件中,因此插件无法隐藏该元素。我已经尝试了以下代码,它对每个人都隐藏了这一点,而不仅仅是客户,所以我不确定我在这里做错了什么。

     <?php if (current_user_can(‘read’)) { ?>
        <div class="dashboard-icon">
            <a id="header-button" href="/creator-dashboard/" class="header-button boss-tooltip" data-tooltip="<?php _e( 'Creator Dashboard', 'onesocial' ); ?>"><i class="fas fa-tachometer-alt"></i></a>
        </div>
    <?php } ?>

请注意,我有能力 'read' 作为测试,因为我无法让它显示。但是供应商的实际能力是 'edit_products'

您可以查看WP_User object, which is returned by the function wp_get_current_user ()里面的用户角色。 因此,您可以通过该代码仅为供应商显示仪表板:

<?php 
$user = wp_get_current_user();
if ( in_array( 'vendor', $user->roles ) ) {
?>
    <!-- Any HTML what you need to hide from "Customers" and show for "Vendor" -->
    <div>Vendor Dashboard</div>
<?php 
}
?>