[英]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.