[英]Show content based on multiple user roles in WordPress
我希望顯示基於多個不同用戶角色的內容。
目的是顯示兩個或三個不同用戶角色的內容,然后將其阻止用於其他用戶角色,顯示一條消息,該消息僅適用於某些已登錄的用戶。
到目前為止,我有以下內容:
<?php
global $user_login, $current_user;
get_currentuserinfo();
$user_info = get_userdata($current_user->ID);
$roles = array (
'administrator',
'subscriber',
);
if (is_user_logged_in() && in_array( $roles, $user_info->roles)) {
//content here
} else {
// they aren't logged in, so show them the login form
}
?>
目前,我遇到的問題似乎是代碼同時尋找管理員和訂戶角色,結果,if語句不滿意,並且顯示了登錄表單。
如果我將$ roles更改為“ administrator”或“ subscriber”,則可以正常工作。
因此,我將如何搜索整個數組以顯示任一角色,而不是全部角色。
謝謝
您可以使用: array_intersect(鏈接)
array_intersect將在兩個數組之間進行檢查,以查看大海撈針( $user_info->roles
)中是否存在針( $roles
$user_info->roles
)。 我已經對此進行了測試,效果很好。
參見下文array_intersect
的使用。
<?php
global $user_login, $current_user;
get_currentuserinfo();
$user_info = get_userdata($current_user->ID);
$roles = array (
'administrator',
'subscriber',
);
if (is_user_logged_in() && array_intersect( $roles, $user_info->roles)) {
echo 'success';
} else {
echo 'failure';
}
?>
示例1: 鏈接 $roles
數組不匹配。
示例2: 鏈接 $roles
數組有一個匹配項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.