设置参数
\'show_in_menu\' => false
使用register\\u post\\u type()注册帖子时,会忽略该帖子类型的管理菜单,因此这是隐藏菜单的最佳解决方案。
如果无法访问它,可以在admin\\u菜单上挂接一个函数,以添加或删除菜单和子菜单。
这里有一个例子。我们确实创建了一个“艺术家”帖子类型,但我们不需要艺术家管理菜单。我们想把它放在“音乐”菜单下;这将有其他各种子菜单。
add_action( \'admin_menu\', \'adjust_admin_menu\' );
function adjust_admin_menu(){
$menu_slug = \'music\'; //menu slug (or path to file); as ID
$post_type_artist_slug = \'artist\';
$post_type_artist = get_post_type_object($post_type_artist_slug);
/////Delete the menu generated by register_post_type() for our custom post type \'artist\'. When registering a post type, setting
$remove_menu_slug = sprintf(\'edit.php?post_type=%s\',$post_type_artist_slug); //menu slug (here, a path to file); as ID
//remove the menu
remove_menu_page( $remove_menu_slug );
/*
//OR remove the \'add new\' submenu
remove_submenu_page(
$remove_menu_slug,
sprintf(\'post-new.php?post_type=%s\',$post_type_artist_slug) //SUBmenu slug (here, a path to file); as ID
);
*/
/////Create our custom menu
$this->menu_page = add_menu_page(
__( \'Music\', \'music-plugin\' ), //page title - I never understood why this parameter is needed for. Put what you like ?
__( \'Music\', \'music-plugin\' ), //menu title
\'manage_options\', //cappability
$menu_slug,
array($this,\'settings_page\'), //this function will output the content of the \'Music\' page.
\'dashicons-album\', // an image would be \'plugins_url( \'myplugin/images/icon.png\' )\'; but for core icons, see https://developer.wordpress.org/resource/dashicons
6
);
////Add submenus
add_submenu_page(
$menu_slug,
$post_type_artist->labels->name, //page title - I never understood why this parameter is needed for. Put what you like ?
$post_type_artist->labels->name, //submenu title
\'edit_posts\',
sprintf(\'edit.php?post_type=%s\',$post_type_artist_slug) //SUBmenu slug (here, a path to a file); as ID
);
add_submenu_page(
$menu_slug,
$post_type_artist->labels->add_new_item,
$post_type_artist->labels->add_new_item,
\'edit_posts\',
sprintf(\'post-new.php?post_type=%s\',$post_type_artist_slug) //SUBmenu slug (here, a path to a file); as ID
);
}