我正在使用主題“Neve”。
我添加了一個自定義函式 my child's theme > functions.php
根據用戶角色,如果用戶是 X 角色,則出現在頭部/導航選單上方的頂部欄將改變顏色。
有人可以建議我可能出錯的地方/為什么在預期的時候不會改變顏色?
親切的問候,
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;
}
}
頂部欄是您看到電話圖示的紅色條帶:

uj5u.com熱心網友回復:
“我需要將此應用于所有頁面而不必使用短代碼”
您可以使用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
}
}
代碼進入functions.php您的活動主題或子主題的檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/364211.html
標籤:php css WordPress的 wordpress 主题 自定义wordpress-pages
