How to Display Custom Post Type Menu as Submenu in WordPress

Home » Snippets » How to Display Custom Post Type Menu as Submenu in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking to create a more organized and streamlined navigation menu for your WordPress website? If so, one effective method is to display your custom post type menu items as submenus. By doing this, you can better categorize your content and make it easier for website visitors to navigate through your site. In this article, we will show you how to display your custom post type menu as a submenu within your WordPress navigation menu.

					function wpturbo_display_custom_post_type_submenu() {
    add_submenu_page(
        'edit.php?post_type=your_custom_post_type',
        __( 'Custom Post Type Submenu', 'wpturbo' ),
        __( 'Custom Post Type Submenu', 'wpturbo' ),
        'manage_options',
        'custom_post_type_submenu',
        'wpturbo_render_custom_post_type_submenu'
    );
}

function wpturbo_render_custom_post_type_submenu() {
    // Your custom post type submenu content goes here
}

add_action( 'admin_menu', 'wpturbo_display_custom_post_type_submenu' );
				

The code above helps you to display a custom post type menu as a submenu in the WordPress admin dashboard. This can be useful if you want to organize different sections of your website and provide easy access to specific custom post types.

First, we define a function called wpturbo_display_custom_post_type_submenu(). Inside this function, we use the add_submenu_page() function to create a submenu page for our custom post type.

The add_submenu_page() function accepts several parameters. Let’s break them down:

  • The first parameter is the slug of the parent menu. In this case, we use 'edit.php?post_type=your_custom_post_type' as the slug. Replace your_custom_post_type with the desired slug of your custom post type that you want to create a submenu for.
  • The second and third parameters are the page title and menu title. We use the __( 'Custom Post Type Submenu', 'wpturbo' ) function to make the titles translatable. You can modify these values to fit your needs.
  • The fourth parameter is the capability required to access the submenu page. In this example, we use 'manage_options', which means only users with the capability to manage options can access the submenu page. Adjust this parameter based on your desired user access level.
  • The fifth parameter is a unique identifier for the submenu page. We use 'custom_post_type_submenu' as the identifier, but you can change it to something else.
  • The sixth parameter is the callback function that will render the content of the submenu page. In our case, the callback function is wpturbo_render_custom_post_type_submenu(). You need to define this function to display the desired content of your submenu page.

Next, we define the wpturbo_render_custom_post_type_submenu() function. This function is responsible for rendering the content of the submenu page. You can customize this function to display any content you want, such as forms, tables, or other elements specific to your custom post type.

Finally, we use the add_action() function to hook the wpturbo_display_custom_post_type_submenu() function into the admin_menu action. This ensures that our custom post type submenu is added to the admin menu.

By using this code snippet, you will be able to create a custom post type submenu in the WordPress admin dashboard, providing easy access to the specific post type and enhancing the organization of your website’s back-end.

Register an account to save your snippets or go Pro to get more features.