简体   繁体   中英

How do I get “posts” to show up in the admin sidebar in wordpress?

I am removing all items that I do not want on the sidebar and I thought it would be easier to tell wordpress what I want to show up instead of what I dont want to show up so that if things get added in the future they wont show up on the side bar by default.

I have added this code and everything is showing up like it should except for posts and the logout link that I have at the bottom. Can anyone help me figure this out? I know I am using the correct slug so Im not sure why its not working.

add_action('admin_init', 'nwcm_admin_init');

 function nwcm_admin_init()
 {   
      if (!current_user_can('editor')) {
      return;
  }
  $menus_to_stay = array(
    'index.php',
     'edit.php',
    'upload.php',
    'edit.php?post_type=page',
   'nav-menus.php',
    'post-new.php',
    'admin.php?page=logout'
       );      
     foreach ($GLOBALS['menu'] as $key => $value) {          
    if (!in_array($value[2], $menus_to_stay)) 
    remove_menu_page($value[2]);
    }   

     } 

I need to do this in functions.php and not with a plugin so please dont recommend plugins!

Your action is not correct, use admin_menu

https://codex.wordpress.org/Function_Reference/remove_menu_page

add_action('admin_menu', 'nwcm_admin_init');
function nwcm_admin_init()
{
    if (!current_user_can('editor'))
        return;

    $menus_to_stay = array(
        'index.php',
        'edit.php',
        'upload.php',
        'edit.php?post_type=page',
        'nav-menus.php',
        'post-new.php',
        'admin.php?page=logout'
    );
    foreach ($GLOBALS['menu'] as $key => $value) {
        if (!in_array($value[2], $menus_to_stay))
            remove_menu_page($value[2]);
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM