admin管理员组

文章数量:1122846

I created a custom post type with custom pages and I would like to add a new taxonomy for this one.

I try to understand how I can add/display a taxonomy subpage using "show_in_menu" => false parameter : necessary to use add_menu_page() and to not display twice menu and submenu.

Currently my menu and subpage display and works correctly but not my taxonomy.

I register a taxonomy for this custom post type and its page works (website/wp-admin/edit-tags.php?taxonomy=stm_section_category&post_type=section_post_type) but no submenu item .

If I use "show_in_menu" parameter with "true" value the category item display in submenu but custom menu display twice!

Does the parameters of add_submenu_page() are wrong or wrong order or I miss to declare something ?

    function stm_section_post_type(){

    $args = array(
          'public' => true,
          'show_in_menu' => false,
          'has_archive' => true,
          'taxonomies' => array( 'post_tag' ),
      );
      register_post_type( 'section_post_type', $args );

    $labels_sections = array(
        'name' => __( 'Catégories de sections'),
        'singular_name' => __( 'catégories sections'),
        'search_items' => __( 'Chercher une catégorie'),
        'all_items' => __( 'Toutes les catégories de section'  ),
        'edit_item' => __( 'Editer'  ),
        'update_item' => __( 'Mettre à jour'),
        'add_new_item' => __( 'Ajouter' ),
        'new_item_name' => __( 'Nouveau nom de la catégorie' ),
        'menu_name' => __( 'Catégories'  ),
    );

    $args_sections = array(

        'labels' => $labels_sections,
        'hierarchical' => true,
        'sort' => true,
        'args' => array( 'orderby' => 'term_order' ),
        'rewrite' => array( 'slug' => 'genres' ),
        'show_admin_column' => true,
        'show_in_rest' => true,
    );
    //'menu_slug'   => 'edit-tags.php?taxonomy=stm_section_category&post_type=section_post_type',

    register_taxonomy( 'stm_section_category' , array( 'section_post_type' ), $args_sections);
    
}
add_action( 'init', 'stm_section_post_type' );

function stm_new_posttype_plugin() {
    add_menu_page(
        'Toutes les sections',
        'Toutes les sections', 
        'manage_options',
        '/edit.php?post_type=section_post_type', 
        'stm_section_list', 
        'dashicons-fas fa-box-archive', 
        6
    );
    add_submenu_page(
        '/edit.php?post_type=section_post_type',  
        'Ajouter une section', 
        'Ajouter une section', 
        'manage_options', 
        'add-section',
        'stm_section_add'
    );
    add_submenu_page(
        '/edit-tags.php?taxonomy=stm_section_category&post_type=section_post_type', 
        'Catégories des sections', 
        'Catégories des sections', 
        'manage_options',
        'category-section',
        null 
    );
}
add_action( 'admin_menu', 'stm_new_posttype_plugin','manage_options');

I created a custom post type with custom pages and I would like to add a new taxonomy for this one.

I try to understand how I can add/display a taxonomy subpage using "show_in_menu" => false parameter : necessary to use add_menu_page() and to not display twice menu and submenu.

Currently my menu and subpage display and works correctly but not my taxonomy.

I register a taxonomy for this custom post type and its page works (website/wp-admin/edit-tags.php?taxonomy=stm_section_category&post_type=section_post_type) but no submenu item .

If I use "show_in_menu" parameter with "true" value the category item display in submenu but custom menu display twice!

Does the parameters of add_submenu_page() are wrong or wrong order or I miss to declare something ?

    function stm_section_post_type(){

    $args = array(
          'public' => true,
          'show_in_menu' => false,
          'has_archive' => true,
          'taxonomies' => array( 'post_tag' ),
      );
      register_post_type( 'section_post_type', $args );

    $labels_sections = array(
        'name' => __( 'Catégories de sections'),
        'singular_name' => __( 'catégories sections'),
        'search_items' => __( 'Chercher une catégorie'),
        'all_items' => __( 'Toutes les catégories de section'  ),
        'edit_item' => __( 'Editer'  ),
        'update_item' => __( 'Mettre à jour'),
        'add_new_item' => __( 'Ajouter' ),
        'new_item_name' => __( 'Nouveau nom de la catégorie' ),
        'menu_name' => __( 'Catégories'  ),
    );

    $args_sections = array(

        'labels' => $labels_sections,
        'hierarchical' => true,
        'sort' => true,
        'args' => array( 'orderby' => 'term_order' ),
        'rewrite' => array( 'slug' => 'genres' ),
        'show_admin_column' => true,
        'show_in_rest' => true,
    );
    //'menu_slug'   => 'edit-tags.php?taxonomy=stm_section_category&post_type=section_post_type',

    register_taxonomy( 'stm_section_category' , array( 'section_post_type' ), $args_sections);
    
}
add_action( 'init', 'stm_section_post_type' );

function stm_new_posttype_plugin() {
    add_menu_page(
        'Toutes les sections',
        'Toutes les sections', 
        'manage_options',
        '/edit.php?post_type=section_post_type', 
        'stm_section_list', 
        'dashicons-fas fa-box-archive', 
        6
    );
    add_submenu_page(
        '/edit.php?post_type=section_post_type',  
        'Ajouter une section', 
        'Ajouter une section', 
        'manage_options', 
        'add-section',
        'stm_section_add'
    );
    add_submenu_page(
        '/edit-tags.php?taxonomy=stm_section_category&post_type=section_post_type', 
        'Catégories des sections', 
        'Catégories des sections', 
        'manage_options',
        'category-section',
        null 
    );
}
add_action( 'admin_menu', 'stm_new_posttype_plugin','manage_options');
Share Improve this question asked Aug 4, 2024 at 14:29 imagIneimagIne 10511 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You don't need to use add_submenu_page() for custom post types and taxonomies, WordPress adds them to the menu automatically when you use the show_in_menu parameter.

本文标签: