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-page
.
Here’s my code:
register_post_type('my_custom_post_type',
array(
'labels' => array(
'name' => __('Books', 'mcpt'),
'singular_name' => __('Book', 'mcpt'),
),
'supports' => array('title', 'editor'),
'show_ui' => true,
'show_in_nav_menus' => false,
'show_in_menu' => 'my-custom-parent-page',
)
);
It works, meaning that it’s properly located under the menu my-custom-parent-page
, however now when I click on the parent menu (i.e. my-custom-parent-page
) it points me to the my_custom_post_type
page…
Any help?
Place a Custom-Post-Type in an submenu of an existing parent page
According to the Codex, this is a known and expected behavior:
Source: https://codex.wordpress.org/Function_Reference/register_post_type#Arguments (See the “show_in_menu” section)
Here is the end of the quote which offers a solution:
So this is quite simple to solve. However in my case I couldn’t change the priority of the parent page because it is generated by a third-party library. Therefore I came up with this solution:
Please note the priority 11, and also when registering the Custom-Post-Type I set the “
show_in_menu
” parameter tofalse
, so we can add it in the menu manually viaadd_submenu_page
as shown above.Properly set the Custom-Post-Type submenu entry as “active”
Now, the above solution works fine, however when creating/editing a post of the “example_cpt” Custom-Post-Type, it is not set as active and the submenu is not unfolded. Here is how to make sure that it is set as active, as well as the submenu in which it resides is properly set as active when creating/editing a post of the “example_cpt” Custom-Post-Type:
Fine-tuning: Rename the first submenu entry
Furthermore, I also wanted the first menu entry of my submenu to be named differently from the parent name. By default, and using the code above, this is what we have:
So as you can see, the first menu entry of the submenu is a duplicate of the parent menu, and this is the default WordPress behavior. I wanted to rename this duplicate entry to something different, much like WordPress does with the default menus (for example “Posts” and the submenu entry “All Posts” which both point to the same page but are named differently).
Here is how to rename the first submenu entry:
Please note the priority 11, so it is renamed after it has been created. And now we have:
Please note that “Submenu Text” points to the same location as “Example Parent Page”.
You also can simply set
'show_in_menu'
in custom post type args to$menu_slug
that you set inadd_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):
Then create custom post type with ‘show_in_menu’ arg set to
menu_slug
that we just set insettings_menu()
function.Hope it helps.
can’t say, what’s exactly the reason, but it seems wordpress redirects to the first submenu-item.
So you have to create a new sub-menu-item with the same contents of your parent-menu-item.
Hope it works for you.