add_submenu_page()出现在子菜单的顶部而不是底部

时间:2017-05-15 10:35:36

标签: php wordpress

我正在使用add_submenu_page向帖子类型添加子级菜单。

问题是子级菜单“批次”出现在帖子类型名称“离线课程”的顶部,但我希望它显示在底部

enter image description here

// Register Custom Post Type
function register_offline_course() {

    $argscourse = array(
        'label'                 => __( 'Offline Course', 'Offline Course' ),
        'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'revisions', ),
        'taxonomies'            => array( 'offline_course_cat' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => false,       
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'course_offline', $argscourse );

    $argsbatch = array(
        'label'                 => __( 'Batch', 'Batch' ),
        'supports'              => array( 'title','revisions', ),
        'taxonomies'            => array( 'offline_course_cat' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => false,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => false,       
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'course_batches', $argsbatch );

    add_submenu_page( 'edit.php?post_type=course_offline', 'Batches', 'Batches',
    'manage_options', 'edit.php?post_type=course_batches', NULL );
}
add_action( 'init', 'register_offline_course', 0 );

3 个答案:

答案 0 :(得分:3)

请在自定义帖子类型功能之外创建add_submenu_page函数,并调用admin_menu动作钩子来创建子菜单,然后它就可以了。

// Register Custom Post Type
function register_offline_course() {

    $argscourse = array(
        'label'                 => __( 'Offline Course', 'Offline Course' ),
        'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'revisions', ),
        'taxonomies'            => array( 'offline_course_cat' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => false,       
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'course_offline', $argscourse );

    $argsbatch = array(
        'label'                 => __( 'Batch', 'Batch' ),
        'supports'              => array( 'title','revisions', ),
        'taxonomies'            => array( 'offline_course_cat' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => false,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => false,       
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'course_batches', $argsbatch );

   }
add_action( 'init', 'register_offline_course', 0 );

function sep1_menuexample_create_menu() {
//create a submenu under Settings
 add_submenu_page( 'edit.php?post_type=course_offline', 'Batches', 'Batches',
    'manage_options', 'edit.php?post_type=course_batches',NULL );}

add_action( 'admin_menu', 'sep1_menuexample_create_menu' );

答案 1 :(得分:0)

设置menu_position => 20或者超过你想要的位置

答案 2 :(得分:0)

针对那些希望将子级菜单显示在中间的人。

我将其发布在这里,因为那是我想要的,而Google引导我转到了此页面。

我希望插件的子菜单如下所示:

  • 物品
  • 类别

(此处有任意数量的子菜单项;由附加组件插入)

  • 设置

最终我要像这样在主插件中编写代码:

//I am creating my plugin's sub-menu in two parts: top with priority = 10 and bottom with priority = 90
add_action('admin_menu', 'myplugin_configure_admin_menu_1',10);
add_action('admin_menu', 'myplugin_configure_admin_menu_2',90);

function myplugin_configure_admin_menu_1(){
   //items      
   add_submenu_page('Parent name', 'item_redirect', 'Items', 'administrator',  'edit.php?post_type=myposstype');

   //categories
   add_submenu_page('Parent name', 'category_redirect', 'Categories', 'administrator',  'edit-tags.php?taxonomy=mycategory&post_type=myposstype');
}

function myplugin_configure_admin_menu_2(){ 
   //settings
    add_submenu_page('Parent name', 'settings', 'Settings', 'administrator',  'admin.php?page=Parent name&tab=welcome');
}

然后,在我的插件中

//priority is 10-90    
add_action('admin_menu', 'myaddon_configure_admin_menu',20);

function myaddon_configure_admin_menu(){
   //add some other sub menu item
}