I added a page menu in admin panel via this code and it works fine.
add_menu_page('Ads Dashboard', 'Ads Dashboard', 'administrator', 'adverts', 'advert_admin_options');
And now I need to add/register post type under this menu, but it doesn’t work properly. Seems something is wrong in my code here is my whole code for both:
function add_ads_post_type(){
register_post_type('ads', array(
'labels' => array(
'name' => __('Adverts', 'theme'),
'singular_name' => __('Adverts', 'theme'),
'menu_name' => __('Adverts', 'theme'),
'add_new' => __('Add Advert Item', 'theme'),
'add_new_item' => __('Add New Advert item', 'theme'),
'edit_item' => __('Edit Advert item', 'theme'),
'new_item' => __('New Advert item', 'theme'),
'view_item' => __('View Advert item', 'theme'),
'search_items' => __('Search Advert items', 'theme'),
'not_found' => __('No Advert found', 'theme'),
'not_found_in_trash' => __('No Advert items found in Trash', 'theme'),
),
'public' => TRUE,
'rewrite' => array('slug' => 'ads', 'with_front' => false),
'has_archive' => true,
'supports' => array('title', 'editor'),
'show_in_menu' => 'admin.php?page=adverts'
));
}
function advert_add_to_menu() {
if (is_admin()) {
add_menu_page('Ads Dashboard', 'Ads Dashboard', 'administrator', 'adverts', 'advert_admin_options');
add_submenu_page('adverts', 'Ads', 'Ads', 10, 'ads-list', 'add_ads_post_type' );
}
}
add_action('admin_menu', 'advert_add_to_menu');
Why so hard guys?
Just use
show_in_menu
arg. like:'show_in_menu' => adverts'
in case of custom menu element whereadverts
is an menu uid.If you want to place into already existed menu element like under another post type menu block then you can use
'show_in_menu' => 'edit.php?post_type={CTP}'
,https://imtiazrayhan.com/multiple-custom-post-types-menu-section/
I know it is old, but maybe this save some one time 🙂
I know this is an old question and the approved answer does work, but I found the following blog post very helpful.
https://shellcreeper.com/how-to-add-wordpress-cpt-admin-menu-as-sub-menu/
For a quick start, use the following. Read the full blog to get the highlighting/active page to work:
show_in_menu args MUST false
Add Submenu to parent
try this : post type is ‘ads’
Here’s an example of adding an option page under a custom post type menu block (see also here):
Please see this for more information : https://codex.wordpress.org/Administration_Menus