Search code examples
wordpressadminsubmenu

wordpress menu and submenu of custom post type


I have following structure of menu and submenu. how is it possible to add a new menu and inside place my custom post type menu with sub option. something i am not doing right. kindly advice.

following is the structure. enter image description here

following is the code

function wpdocs_register_my_custom_menu_page(){
    add_menu_page( 
        __( 'Custom Menu Title', 'textdomain' ),
        'SANHA Menu',
        'manage_options',
        'custompage',
        'my_custom_menu_page',
        plugins_url( 'myplugin/images/small-flag.png' ),
        6
    ); 
}
add_action( 'admin_menu', 'wpdocs_register_my_custom_menu_page' );


add_action( 'init', 'salman' );
function salman(){
  register_post_type( 'salu',
    array(
      'labels' => array(
        'name' => __( 'Salmans' ),
        'singular_name' => __( 'salman' )
      ),
      'public' => true,
      'has_archive' => true,
      'menu_icon' => 'dashicons-media-text',
      'hierarchical'      => true,
      'show_ui'           => true,
      'show_admin_column' => true,
      'show_in_menu' => 'edit.php',
      'query_var'         => true,
      'rewrite' => array('slug' => 'restaurants',),
      'supports'=> array( 'title', 'editor', 'excerpt', 'author', 'thumbnail',),
    )
  );
}

Solution

  • You need register your custom post type and have to provide $args for your post type so it shows all the option in the admin panel and it's known as a custom post type, you can follow the below code to have a better understanding

    function my_custom_post_sanha() {
    $labels = array(
    'name'               => _x( 'SANHA Menu', 'post type general name' ),
    'singular_name'      => _x( 'SANHA Menu', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New SANHA Menu' ),
    'edit_item'          => __( 'Edit SANHA Menu' ),
    'new_item'           => __( 'New SANHA Menu' ),
    'all_items'          => __( 'All SANHA Menu' ),
    'view_item'          => __( 'View SANHA Menu' ),
    'search_items'       => __( 'Search SANHA Menu' ),
    'not_found'          => __( 'No SANHA Menu found' ),
    'not_found_in_trash' => __( 'No SANHA Menu found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_name'          => 'SANHA Menu'
    );
    
    $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our SANHA Menu and SANHA Menu specific data',
    'public'        => true,
    'menu_position' => 15,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
    );
    
    register_post_type( 'sanha_menu', $args ); 
    }
    
    add_action( 'init', 'my_custom_post_sanha' );
    
    function my_taxonomies_sanha() {
      $labels = array(
    'name'              => _x( 'SANHA Menu Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'SANHA Menu Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search SANHA Menu Categories' ),
    'all_items'         => __( 'All SANHA Menu Categories' ),
    'parent_item'       => __( 'Parent SANHA Menu Category' ),
    'parent_item_colon' => __( 'Parent SANHA Menu Category:' ),
    'edit_item'         => __( 'Edit SANHA Menu Category' ), 
    'update_item'       => __( 'Update SANHA Menu Category' ),
    'add_new_item'      => __( 'Add New SANHA Menu Category' ),
    'new_item_name'     => __( 'New SANHA Menu Category' ),
    'menu_name'         => __( 'SANHA Menu Categories' ),
    );
    $args = array(
     'labels' => $labels,
     'hierarchical' => true,
    );
    register_taxonomy( 'sanha_category', 'sanha_menu', $args );
    register_taxonomy( 'sanha_tag', 'sanha_menu' );
    }
    add_action( 'init', 'my_taxonomies_sanha', 0 );