Create submenus under Custom Post Type menu in WordPress

Tạo menu con dưới menu Custom Post Type trong WordPress

This content provides a detailed guide on creating a submenu below a custom post type menu in WordPress. It explains the process of registering a custom post type and adding a submenu using code snippets. The example code demonstrates how to register a new custom post type for events and then create a submenu under the “Events” menu. The article concludes by encouraging readers to apply the solution in their WordPress development projects and sharing feedback. Overall, it serves as a helpful resource for developers looking to enhance their WordPress themes or plugins with custom post type submenus.

To create a submenu under your custom post type menu in WordPress, you first need to register a custom post type. This can be easily achieved by adding a code snippet to your theme or child theme’s functions.php file.

Register custom post type

Before creating the submenu, make sure to register a new custom post type using the following code snippet:

// Register Custom Post Type
function event_post_type() {

  $labels = array(
    'name'                  => _x( 'Events', 'Post Type General Name', 'hocwordpress' ),
    'singular_name'         => _x( 'Event', 'Post Type Singular Name', 'hocwordpress' ),
    // Add more labels here
  );

  $args = array(
    'label'                 => __( 'Event', 'hocwordpress' ),
    // Add more arguments here
  );

  register_post_type( 'event', $args );

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

After registering a custom post type, you will see a new menu called "Events" in your WordPress admin dashboard.

Add submenu for custom post type menu

To create a submenu under the "Events" menu, use the add_submenu_page() function. Add the following code below the post type registration code in your functions.php file:

// Hook
add_action('admin_menu', 'hk_demo_submenu_event');

// admin_menu callback function
function hk_demo_submenu_event(){

  add_submenu_page(
    'edit.php?post_type=event', // Slug of parent menu
    'Demo Title', // Page title
    'Demo Menu', // Menu title
    'manage_options',
    'demo_menu', // Slug of submenu
    'hk_render_page' // Callback function
  );

}

// add_submenu_page callback function
function hk_render_page() {
  echo '<h2>HocWordPress.vn</h2>';
}

By adding this code, you will create a submenu under the "Events" menu in your WordPress admin dashboard. The result will be as shown in the image above, with annotations explaining the elements of the add_submenu_page() function.

See also  Preventing Local Attacks on Your WordPress Website

Epilogue

I hope you find this solution helpful in your WordPress theme or plugin development journey, especially when creating settings pages for custom post types. If you have any feedback or questions, please feel free to comment and share this article. Stay updated with more WordPress tips by following us on Facebook.

Remember to rate this article and share your thoughts! Happy WordPress developing!

Rate this post

Related posts