2016-01-28 55 views
1

我正在尋找基於多個不同用戶角色顯示內容。根據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語句不滿意並且登錄表單顯示。

如果我將$角色更改爲'管理員'或'訂戶',那麼它工作正常。

那麼,我將如何搜索數組以顯示其中的任何一個角色,而不是全部。

感謝

+0

請參閱http://stackoverflow.com/questions/26556925/show-content-based-on-logged-in-user-role-in-wordpress-with-php –

+0

嗨@ rahul-s,感謝您的迴應但我不確定你完全閱讀我在找什麼。這是我正在使用的代碼,但我需要它用於多個用戶角色,而不僅僅是一個。該帖子僅供管理員使用。謝謝 – damienoneill2001

+0

是的,這是正確的。 –

回答

3

你可以使用: array_intersect (link)

array_intersect將在兩個陣列之間的檢查,看看是否針($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:LINK$roles陣列不匹配。

例2:LINK$roles數組有一個匹配。

+0

嗨@傑米 - 英鎊,現貨,正是我需要的。我現在也在爲此工作,感謝您的幫助! – damienoneill2001

+0

@ damienoneill2001,樂意幫忙! –

相關問題