Web/ Design/ Vista

Resources for Designers & Developer

Custom Post Type Menu as Sub-menu in WordPress Admin Panel

Ok this needs a bit of more explanation. When you create a custom post type in wordpress, the menu link for this custom post type appears at the top-level in dashboard.

For example – i have a custom post type ‘Intros’ which has a menu link at the top level. But what if i do not want this menu link at the top level – but want it as a sub-menu for another top level menu?

Adding a custom post type menu as sub-menu for another top level menu is quite simple. All you need to do is edit the value of show_in_menu while adding the custom post.

// Default value
'show_in_menu'       => true,

Replace this with

'show_in_menu' => 'themes.php'

This will shift the custom post menu under Appearance menu. See the image below for illustration

Why did i use themes.php? Because this is the menu slug for Appearance menu – that is where i wanted to shift the intros menu.

Use the same method to move your custom post under any other menu. For example – to shift your custom posts menu under posts, use this –

'show_in_menu' => 'edit.php'

Does it work for custom admin menus which are added through add_menu_page function? It does! Let me illustrate –

/**
 * Register a custom menu page.
 */
function wpdocs_register_my_custom_menu_page() {
    add_menu_page(
        __( 'Custom Menu Title', 'textdomain' ),
        'custom menu',
        'manage_options',
        'myplugin/myplugin-admin.php',
        '',
        plugins_url( 'myplugin/images/icon.png' ),
        6
    );
}
add_action( 'admin_menu', 'wpdocs_register_my_custom_menu_page' );

This code above generates a top level menu – custom menu as shown below

To shift my custom post intros menu under custom menu, i changed the show_in_menu to

'show_in_menu'       => 'myplugin/myplugin-admin.php',

where myplugin/myplugin-admin.php is the menu slug for custom menu.

Now intros shows up nicely under custom menu

This concept can probably be applied to sub menus as well. I have not tried that yet.

Did that work for you? Let me know in the comments below.