如何从wordpress管理端隐藏页面


How to hide pages from wordpress admin side

在我的WordPress网站上有几个管理员角色,但我需要在管理面板中从选定的管理员角色中隐藏一些页面,所以我只是在寻找从WordPress管理部分隐藏一些页面的方法。但有些角色可能需要显示隐藏页面。

我只是添加了一个示例图像来了解这一点。根据图片,我想从管理面板中的ABC管理员角色隐藏关于联系页面,并且它应该对管理面板中XYZ管理员角色可见。希望你们能帮忙。

样本图像

对于角色,请尝试以下代码使用数组中的页面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;
}

将此代码放入函数.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;
}

请尝试此代码。。仅插入要排除的页面和要排除的管理员IDadd_action('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;
    }