简体   繁体   English

Wordpress 如何根据每个页面上的用户角色应用特定的 CSS 属性

[英]Wordpress how to apply specific CSS property based on user role on every page

I am using the theme "Neve".我正在使用主题“Neve”。

I have added a custom function to my child's theme > functions.php我在my child's theme > functions.php

Based on the user role, if the user is X role the topbar which appears above the head/nav menu will change colour.根据用户角色,如果用户是 X 角色,则显示在 head/nav 菜单上方的顶栏将改变颜色。

Can someone advise where I may have gone wrong / as to why this is not changing colour when expected to do so?有人可以建议我可能在哪里出错/为什么在预期这样做时它没有改变颜色?

Kind Regards,亲切的问候,

function topbar_switcher () {
    
    $current_user = wp_get_current_user();
    
    switch (true)  {
    case ( user_can( $current_user, "subscriber") ):
        ?>
            <style> 
                .header-top {
                background-color:black;
                }
            </style>
        <?php
    break;
 case ( user_can( $current_user, "customer") ):
        ?>
            <style> 
                .header-top {
                    background-color:#00337f;
                }
            </style>
        <?php
    break;
 case ( user_can( $current_user, "administrator") ):
    ?>
            <style> 
                .header-top {
                    background-color:yellow;
                }
            </style>
        <?php
 break;
            
    }   
}

Top bar is the red strip where you see the phone icon:顶栏是您看到电话图标的红色条:

在此处输入图像描述

"I need this to apply to all pages without having to use shortcode" “我需要将其应用于所有页面,而无需使用简码”

You could use the wp_head action hook to run the following code for every single page without using a shortcode.您可以使用wp_head操作挂钩为每个页面运行以下代码,而无需使用简码。

add_action('wp_head', 'changing_color_of_top_navbar');

function changing_color_of_top_navbar()
{

    $current_user = wp_get_current_user();

    $user_roles = $current_user->roles;

    if (in_array("subscriber", $user_roles)) {
        ?>
        <style>
            .header-top {
                background-color: black;
            }
        </style>
    <?php
    } elseif (in_array("customer", $user_roles)) {
    ?>
        <style>
            .header-top {
                background-color: #00337f;
            }
        </style>
    <?php
    } elseif (in_array("administrator", $user_roles)) {
    ?>
        <style>
            .header-top {
                background-color: yellow;
            }
        </style>
    <?php
    }
}

Code goes into the functions.php file of your active theme or child theme.代码进入您的活动主题或子主题的functions.php文件。

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

相关问题 隐藏特定用户角色的 class CSS Wordpress - Hide class CSS for specific user role Wordpress 根据用户角色和页面显示 header (Wordpress) - Display header based on user role and page (Wordpress) 如何根据角色将用户重定向到特定网页? - How to redirect user based on role to a specific web page? 访问页面时根据用户角色重定向 WordPress 用户 - Redirect WordPress User based on user role when accessing a page WordPress:不在特定用户角色中时将主页和页面重定向到帐户页面 - WordPress: Redirect homepage and pages to account page when not in a specific user role 如何使用php根据用户角色更改WordPress管理页面的背景颜色? - How to change background colour of WordPress admin page based on user role using php? Laravel 5 - 根据用户在身份验证中的角色将用户重定向到特定页面 - Laravel 5 - Redirecting a user to a specific page based on their role on authentication 如何为总购物车中的特定用户角色应用折扣但不为运费应用折扣? - How to apply a discount for a specific user role in total cart but not for the shipping cost? 如何删除特定于用户角色的wordpress管理员菜单项? - How to remove wordpress admin menu items specific to a user role? 如何从特定用户角色 wordpress 中删除 tinyMCE 按钮 - How to remove tinyMCE button from specific user role wordpress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM