在新菜单下删除或移动管理子菜单

时间:2016-08-23 作者:gordie

我正在编写一个新的Wordpress插件,它可以注册几种新的帖子类型。它们在Wordpress后端都有自己的菜单和子菜单。如何创建一个新菜单(如父类别)并将其下的所有内容移动?

1 个回复
SO网友:gordie

设置参数

    \'show_in_menu\'  => false
使用register\\u post\\u type()注册帖子时,会忽略该帖子类型的管理菜单,因此这是隐藏菜单的最佳解决方案。

如果无法访问它,可以在admin\\u菜单上挂接一个函数,以添加或删除菜单和子菜单。

这里有一个例子。我们确实创建了一个“艺术家”帖子类型,但我们不需要艺术家管理菜单。我们想把它放在“音乐”菜单下;这将有其他各种子菜单。

    add_action( \'admin_menu\', \'adjust_admin_menu\' );

    function adjust_admin_menu(){

        $menu_slug = \'music\'; //menu slug (or path to file); as ID
        $post_type_artist_slug = \'artist\';
        $post_type_artist = get_post_type_object($post_type_artist_slug);

        /////Delete the menu generated by register_post_type() for our custom post type \'artist\'.  When registering a post type, setting 

        $remove_menu_slug = sprintf(\'edit.php?post_type=%s\',$post_type_artist_slug); //menu slug (here, a path to file); as ID

        //remove the menu
        remove_menu_page( $remove_menu_slug );
        /*
        //OR remove the \'add new\' submenu
        remove_submenu_page( 
             $remove_menu_slug,
            sprintf(\'post-new.php?post_type=%s\',$post_type_artist_slug) //SUBmenu slug (here, a path to file); as ID
        );
        */
        /////Create our custom menu

        $this->menu_page = add_menu_page( 
            __( \'Music\', \'music-plugin\' ), //page title - I never understood why this parameter is needed for.  Put what you like ?
            __( \'Music\', \'music-plugin\' ), //menu title
            \'manage_options\', //cappability
            $menu_slug,
            array($this,\'settings_page\'), //this function will output the content of the \'Music\' page.
            \'dashicons-album\', // an image would be \'plugins_url( \'myplugin/images/icon.png\' )\'; but for core icons, see https://developer.wordpress.org/resource/dashicons 
            6
        ); 

        ////Add submenus

         add_submenu_page(
                $menu_slug,
                $post_type_artist->labels->name, //page title - I never understood why this parameter is needed for.  Put what you like ?
                $post_type_artist->labels->name, //submenu title
                \'edit_posts\',
                sprintf(\'edit.php?post_type=%s\',$post_type_artist_slug) //SUBmenu slug (here, a path to a file); as ID

         );

         add_submenu_page(
                $menu_slug,
                $post_type_artist->labels->add_new_item,
                $post_type_artist->labels->add_new_item,
                \'edit_posts\',
                sprintf(\'post-new.php?post_type=%s\',$post_type_artist_slug) //SUBmenu slug (here, a path to a file); as ID

         );
    }

相关推荐

在将代码添加到函数后无法登录WordPress wp-admin。php

我在函数末尾添加以下代码。php文件,用于根据自定义帖子的帖子标题填充分类法。问题是,当我添加代码时,尝试登录wp admin时会出现以下错误。非常感谢您能帮助我们弄清楚为什么会发生这种情况。Error:错误:由于意外输出,Cookie被阻止。有关帮助,请参阅此文档或尝试支持论坛。Code: <?php function update_custom_terms($post_id) { // only update terms if