WordPress Admin: When placing a Custom Post Type as a submenu of a parent menu, the parent menu link is being overridden by the CPT

后端 未结 3 2011
慢半拍i
慢半拍i 2021-02-04 20:46

I register a Custom Post Type, and I don\'t want it to have its own menu, instead I want to place it as a submenu of an existing admin menu item called my-custom-parent-pa

3条回答
  •  渐次进展
    2021-02-04 21:23

    You also can simply set 'show_in_menu' in custom post type args to $menu_slug that you set in add_menu_page() that you want to set the CPT as sub menu of and set the priority of admin_menu function to 9 or lower. For example:

    First, create a new top-level menu page, with priority set to 9 or lower (it's a must):

    add_action( 'admin_menu', 'settings_menu' ), 9 );
    
    function settings_menu() {
    
        add_menu_page( __( 'Page Title' ), 'Menu Title', 'manage_options', 'menu_slug', show_page_callback() );
    }
    
    function show_page_callback() {
    
        // show the settings page, plugin homepage, etc.
    }
    

    Then create custom post type with 'show_in_menu' arg set to menu_slug that we just set in settings_menu() function.

    add_action( 'init', 'create_post_type' );
    
    function create_post_type() {
    
    register_post_type('my_custom_post_type',
        array(
            'labels' => array(              
                'name'               => __('Books', 'mcpt'),
                'singular_name'      => __('Book', 'mcpt'),
            ),
            'supports' => array('title', 'editor'),
            'public' => true,
            'show_in_menu' => 'menu_slug',
        );
    }
    

    Hope it helps.

提交回复
热议问题