在我的WordPress网站上有几个管理员角色,但我需要隐藏管理面板中选定管理员角色的一些页面,所以我只是想搜索隐藏WordPress管理部分中的一些页面 . 但是某些角色可能需要显示隐藏页面 .
我只是添加一个示例图像来了解它 . 根据图像我想在管理面板中隐藏ABC管理员角色的 about 和 contact 页面,它应该对管理面板中的XYZ管理员角色可见 . 希望你们能帮忙 .
Sample Image
对于角色,请尝试使用此代码在数组中使用页面ID .
add_action( 'pre_get_posts' ,'exclude_this_page' ); function exclude_this_page( $query ) { global $pagenow; global $current_user; $user_roles = $current_user->roles; if( $user_roles[0] == 'administrator' ){ return $query; } if($user_roles[0] == 'editor'){ if( 'edit.php' == $pagenow && 'page' == get_query_var('post_type') ) $query->set( 'post__not_in', array(20,25) ); } return $query; }
将此代码放在function.php文件中
add_action( 'pre_get_posts' ,'exclude_this_page' ); function exclude_this_page( $query ) { $user_id = get_current_user_id(); // If XYZ admin id is 23 if($user_id != 23 ) return $query; global $pagenow; if( 'edit.php' == $pagenow && ( get_query_var('post_type') && 'page' == get_query_var('post_type') ) ) $query->set( 'post__not_in', array(10,14) ); // array page ids(contact us and abous us page ids) return $query; }
请尝试此代码..仅插入要排除的页面和要从add_action中排除的管理员ID('pre_get_posts','exclude_this_page');
function exclude_this_page( $query ) { //current user ID $user_id = get_current_user_id(); //Ids of users to exclude $excluded_users = array(1,2,3); //Ids of pages to be excluded $excluded_pages = array(10,20,30); global $pagenow; if( 'edit.php' == $pagenow && ( get_query_var('post_type') && 'page' == get_query_var('post_type') ) ){ if(in_array($user_id,$excluded_users)){ $query->set( 'post__not_in', $excluded_pages ); } } return $query; }
3 回答
对于角色,请尝试使用此代码在数组中使用页面ID .
将此代码放在function.php文件中
请尝试此代码..仅插入要排除的页面和要从add_action中排除的管理员ID('pre_get_posts','exclude_this_page');