WordPress Admin: When placing a Custom Post Type as a submenu of a parent menu, the parent menu link is being overridden by the CPT

后端 未结 3 2009
慢半拍i
慢半拍i 2021-02-04 20:46

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-pa

3条回答
  •  广开言路
    2021-02-04 21:34

    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.

    add_action('admin_menu', 'my_admin_menu');
    
    function my_admin_menu() {
        global $submenu;
        add_menu_page('My Menu', 'My Menu', 'administrator', 'my-menu', 'callback_func');
    
        $parent = array('My Menu', 'administrator', 'my-menu', 'My Menu'); // new submenu-itm
        $submenu['my-menu'] = fix_menu($submenu['my-menu'], $parent); // adds the new submenu-item at beginning of 'my-menu'-item
    }
    
    function fix_menu($submenu) {
        array_unshift ($submenu, $parent);
        return $submenu;
    }
    

    Hope it works for you.

提交回复
热议问题