X

Display Custom Post Type Menu as Submenu

Snippets by IsItWP

Do you want to display the custom post type as a submenu within the post edit menu? While there’s probably a plugin for this, we have created a quick code snippet that you can use display custom post type menu as submenu in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

function nacin_register_slideshows_post_type() {
	register_post_type( 'slideshow', array(
		'labels' => array(
			'name' => 'Slideshows',
			'singular_name' => 'Slideshow',
		),
		'public' => true,
		'show_ui' => true,
		'show_in_menu' => 'edit.php',
		'supports' => array( 'title' ,'thumbnail', 'editor' ),
	) );
}
add_action( 'init', 'nacin_register_slideshows_post_type' );

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 43 best photography themes for WordPress and how to create a contact form in WordPress.

Comments   Leave a Reply

  1. Mochammad Taufiq November 26, 2013 at 2:12 am

    i register post_type with add new item, but ‘add new item’ not show in submenu, any suggestion for my problem ?

  2. Nice. Trying to take this a step further and attach the post type to a custom menu page. So far no cigar… any thoughts?

    1. Your question is old but figured this might come in handy for someone else.
      I just spent a long time looking through the WP source code trying to do the
      same thing: I have a custom menu created using add_menu_page(). It has a handful
      of sub_menu items. I have a custom post type that is related to this menu and I
      wanted the add / list links for that post type to be submenu items.

      Short answer: No easy way to do it in WP. The way it
      generates the menus and custom post type UI means there is no simple call you
      can make.

      Solution:
      1. Register your post type with the
      following options:
      ‘show_ui’ => true,
      ‘show_in_menu’ => false
      In
      this example my custom post type is acct_notes

      2. In your theme’s functions.php file or your plugin file, add an action to
      admin_menu:
      add_action(‘admin_menu’,
      ‘modify_admin_menus’);

      3. Add submenu_pages for the two post UI links — list posts, create new
      post.
      The first param is the slug of the menu you’re adding it to. The second
      is the name that will show up on the page. The third is what the link will show
      up as. The fourth is the capability required to access it. The fifth is the slug
      for this menu item. And last is the function that will generate the output. In
      this case we’re not going to use it so we can pass NULL.

      add_submenu_page(‘members’, ‘Account Notes’, ‘Account Notes’,
      ‘manage-options’, ‘view_account_notes’, NULL);

      4. The last step is the function that will change those submenu links so
      that they go to the correct post UI pages.

      function modify_admin_menus(){
      global $submenu;

      if(array_key_exists(‘members’, $submenu)){

      foreach($submenu[‘members’] as $key => $value){
      $k =
      array_search(‘view_account_notes’, $value);
      if($k){

      $submenu[‘members’][$key][$k] =
      (current_user_can($submenu[‘members’][$key][1]))?
      admin_url(‘/edit.php?post_type=acct_notes’):”;
      }

      $l = array_search(‘new_account_note’, $value);

      if($l){
      $submenu[‘members’][$key][$l] =
      (current_user_can($submenu[‘members’][$key][1]))?
      admin_url(‘/post-new.php?post_type=dojo_acct_notes’) : ”;}

      }
      }
      }

      What’s going on in the function is that it’s checking the global $submenu
      array to see if our top level menu item (members) exists — it might not if the
      user doesn’t have permission to access it. If they do it then looks to see if
      that top level menu item has a submenu page with the slug “view_account_notes”
      — what we assigned in add submenu_page. Finally, it checks the permission we
      assigned in add_submenu_page and if the current user has that permission it
      outputs the new link. If they don’t it removes the link altogether.

      We do that same check twice — once for the link to list our custom post
      type, and once for the link to add a new custom post type.

      It would be far preferable to have this capability built in, but this will
      let you add custom post type user interfaces to any menu you want.

  3. Thank you very much. Can you help me, please? I am having problem: I want create a menu in WordPress admin contained many child custom post type to easy to manage. Example:

    Menu Facilities include post types: – Meetings; – Weddings; – SPA

    Thanks for help very much.

  4. good hack.thanks.

    1. no problem anytime,

    2.  It’s not a hack, it’s a feature

Add a Comment Cancel reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!