Reputation: 1429
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');
Upvotes: 1
Views: 4521
Reputation: 1729
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
register_post_type( 'your-cpt', array(
'description' => '',
'public' => true,
'show_ui' => true,
'show_in_menu' => false, /* Do not show admin menu */
[...]
) );
Add Submenu to parent
add_submenu_page(
'Ads_Dashboard', // parent slug
'Your CPT Title', // page title
'Sub Menu Title', // sub-menu title
'edit_posts', // capability
'edit.php?post_type=your-cpt' // your menu menu slug
);
Upvotes: 3
Reputation: 362
Why so hard guys?
Just use show_in_menu
arg. like: 'show_in_menu' => adverts'
in case of custom menu element where adverts
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 :)
Upvotes: 4
Reputation: 4270
<?php
add_menu_page('Page title', 'Top-level menu title', 'manage_options', 'my-top-level-handle', 'my_magic_function');
add_submenu_page( 'my-top-level-handle', 'Page title', 'Sub-menu title', 'manage_options', 'my-submenu-handle', 'my_magic_function');
?>
Here's an example of adding an option page under a custom post type menu block (see also here):
<?php
add_submenu_page('edit.php?post_type=wiki', 'Options', 'Options', 'manage_options', 'wiki-options', array(&$this, 'options_page') );
?>
Please see this for more information : https://codex.wordpress.org/Administration_Menus
Upvotes: 1
Reputation: 2942
try this : post type is 'ads'
//REMOVE MENU
function remove_menu_CPT_ads() {
remove_menu_page( 'edit.php?post_type=ads' );
}
add_action( 'admin_menu', 'remove_menu_CPT_ads' );
function add_my_menu(){
add_submenu_page('adverts', 'Ads', 'Ads', 'manage_options', 'my-top-level-slug','Your_function');
}
add_action('admin_menu','add_my_menu');
function Your_function(){
wp_redirect(admin_url().'/edit.php?post_type=ads');
exit;
}
function add_ob_start(){
if(is_admin()){
ob_start();
}
}
add_action('admin_init', 'add_ob_start');
Upvotes: 1