If Conditional 有两个角色
If Conditional with two roles
我一直在努力,但我没有取得成果。
如果不重定向页面,我希望只有 post 的作者和具有 shop_manager 角色的用户才能看到它。如果他们是作者但他们没有写 post,他们将无法访问它。
除了 shop_manager 用户可以看到之外,该代码适用于所有内容。
function wpso_author_redirection() {
if ( is_single() ) {
$current_post_details = get_post( get_the_ID(), ARRAY_A );
$user_id = get_current_user_id();
if ($user_id != absint( $current_post_details['post_author']) ||$user
['roles']!= 'shop_manager' ) {
wp_redirect( 'https://aa.com/contenido-bloqueado');
}
}
}
在最里面的 if 语句中,您正在使用 || .这意味着 OR,这意味着如果用户不是作者或用户角色不是 shop_manager,将执行重定向。
您必须在 php 中使用 AND、&&:
if ($user_id != absint( $current_post_details['post_author']) && $user['roles']!= 'shop_manager' ) {
wp_redirect( 'https://aa.com/contenido-bloqueado');
}
这个 AND 表示如果用户不是作者并且用户角色不是 shop_manager 则执行重定向。如果两者中的任何一个为假(用户是作者或用户角色是 shop_manager),则不会执行重定向。
我一直在努力,但我没有取得成果。 如果不重定向页面,我希望只有 post 的作者和具有 shop_manager 角色的用户才能看到它。如果他们是作者但他们没有写 post,他们将无法访问它。
除了 shop_manager 用户可以看到之外,该代码适用于所有内容。
function wpso_author_redirection() {
if ( is_single() ) {
$current_post_details = get_post( get_the_ID(), ARRAY_A );
$user_id = get_current_user_id();
if ($user_id != absint( $current_post_details['post_author']) ||$user
['roles']!= 'shop_manager' ) {
wp_redirect( 'https://aa.com/contenido-bloqueado');
}
}
}
在最里面的 if 语句中,您正在使用 || .这意味着 OR,这意味着如果用户不是作者或用户角色不是 shop_manager,将执行重定向。
您必须在 php 中使用 AND、&&:
if ($user_id != absint( $current_post_details['post_author']) && $user['roles']!= 'shop_manager' ) {
wp_redirect( 'https://aa.com/contenido-bloqueado');
}
这个 AND 表示如果用户不是作者并且用户角色不是 shop_manager 则执行重定向。如果两者中的任何一个为假(用户是作者或用户角色是 shop_manager),则不会执行重定向。