简体   繁体   English

WordPress:如何隐藏自定义用户角色的某个菜单项?

[英]WordPress: How do I hide a certain menu item for a custom user role?

I am looking to have some CSS trigger based on whether a WordPress user has a certain user role assigned.我希望根据 WordPress 用户是否分配了特定的用户角色来设置一些 CSS 触发器。

I've linked below the code I thought would allow certain CSS to trigger based on a user role.我在我认为允许某些 CSS 基于用户角色触发的代码下方进行了链接。

function wpmu_role_based_style() {
    if ( current_user_can( 'wholesale_customer' ) ) { ?>
    
        <style>
            .wholesale-shop {display:none;}
        </style>
    <?php  } ?>
}

You can add body classes for different user roles by using body_class filter.您可以使用body_class过滤器为不同的用户角色添加正文类。 For example,例如,

add_filter( 'body_class', 'body_class_user_role' );

function body_class_user_role( $classes ) {

    if( is_user_logged_in() ) {

        $user_role = wp_get_current_user()->roles;
        $classes[] = 'user-role-' . $user_role[0];

    } 
    return $classes;
}

Then use the class like below example:然后使用如下示例所示的类:

.logged-in.user-role-wholesale_customer .wholesale-shop { 
    display: none; 
}

You can add the CSS to the custom theme or the " Additional CSS " section of Customizer.您可以将 CSS 添加到自定义主题或自定义程序的“附加 CSS ”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM