5

I have a custom menu using add_menu_page:

add_menu_page('My menu' , 'Some text', 'read', 'nwcm');

Under it, I show a custom post type menu item;

// Create the news custom post type
register_post_type('nwcm_news', array(
    'labels' => array(
        'name' => __('News for clients', NWCM_TEXT_DOMAIN) ,
        'singular_name' => __('News', NWCM_TEXT_DOMAIN)
    ) ,
    'public' => true,
    'has_archive' => true,
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => 'nwcm',
    'taxonomies' => array(
        'nwcm_news_category'
    ) ,
));

Then I add a custom taxonomy hooked to that "nwcm_news" post type:

// register news taxonomy
register_taxonomy('nwcm_news_category', 'nwcm_news', array(
    'label' => __('News categories') ,
    'menu_name' => __('News categories') ,
    'rewrite' => array(
        'slug' => 'nwcm_news_category'
    ) ,
    'hierarchical' => true
));

The parent menu and the custom post type both show correctly... but, the taxonomy menu isn't showing :(

How can I solve this? I had checked this solution but the answer lacks the full code example..

12

You have some messed up code.

I have reformatted your code to code which actually works.

The following solution allows you to give your Custom Post Type menu a menu name of what ever you want. Just change the label "menu_name".

POST TYPE

// Create the news custom post type
register_post_type('nwcm_news', array(
    'labels'        => array(
        'name'          => __('News for clients', 'NWCM'),
        'singular_name' => __('News', 'NWCM'),
        'menu_name'     => __('NWCM', 'NWCM'),
        'all_items'     => __('View Articles', 'NWCM'),
    ),
    'public'        => true,
    'has_archive'   => true,
    'show_ui'       => true,
    'show_in_menu'  => true,
    'taxonomies'    => array(
        'nwcm_news_category'
    )
));

TAXONOMY

// register news taxonomy
register_taxonomy('nwcm_news_category', 'nwcm_news', array(
    'label'         => 'News Categories',
    'labels'        => array(
        'menu_name' => __('News Categories', 'NWCM')
    ),
    'rewrite'       => array(
        'slug' => 'nwcm-news-category'
    ),
    'hierarchical'  => true
));

I'm not 100% sure if you are wanting your own custom admin menu to add your stuff under, or if you just want to change the menu name of the custom post type.

I have added in the menu_name of "NWCM" to the labels of your custom post type.

I would highly recommend you read through and fully understand the parameters and arguments for registering custom post types and taxonomies.


EDIT: 09/05/2014

If you wanted to completely add your own custom admin menu, and mix in your Custom Post Types, Custom Taxonomies, and any other custom admin pages of your own... The following solution works. Please note, it's just a starting point and you don't have to do it this way 100% to a "T". It's just an example... I recommend you modify it, so that it's understandable and maintainable by you or your developer.

Hook into init and register Custom Post Types and Custom Taxonomies.

if ( ! function_exists( 'mbe_init' ) ) {

    function mbe_init() {

        # Create the news custom post type
        register_post_type( 'nwcm_news', array(
            'labels'       => array(
                'name'          => __( 'News for clients', 'NWCM' ),
                'singular_name' => __( 'News', 'NWCM' ),
            ),
            'public'       => true,
            'has_archive'  => true,
            'show_ui'      => true,
            'show_in_menu' => false,// adding to custom menu manually
            'taxonomies'   => array(
                'nwcm_news_category'
            )
        ) );

        # Create the news categories custom taxonomy
        register_taxonomy( 'nwcm_news_category', 'nwcm_news', array(
            'label'        => 'News Categories',
            'labels'       => array(
                'menu_name' => __( 'News Categories', 'NWCM' )
            ),
            'rewrite'      => array(
                'slug' => 'nwcm-news-category'
            ),
            'hierarchical' => true
        ) );

    }

    add_action( 'init', 'mbe_init' );

}

Hook into admin_menu to create a custom parent admin menu, and add Custom Submenu Admin Pages, Custom Post Type pages, and Custom Taxonomy Pages all to the custom parent admin menu.

if ( ! function_exists( 'mbe_add_admin_menu' ) && ! function_exists( 'mbe_display_admin_page' ) ) {

    function mbe_add_admin_menus() {

        # Settings for custom admin menu
        $page_title = 'News for clients';
        $menu_title = 'NWCM';
        $capability = 'post';
        $menu_slug  = 'nwcm';
        $function   = 'mbe_display_admin_page';// Callback function which displays the page content.
        $icon_url   = 'dashicons-admin-page';
        $position   = 0;

        # Add custom admin menu
        add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );

        $submenu_pages = array(

            # Avoid duplicate pages. Add submenu page with same slug as parent slug.
            array(
                'parent_slug' => 'nwcm',
                'page_title'  => 'Summary of News',
                'menu_title'  => 'Summary',
                'capability'  => 'read',
                'menu_slug'   => 'nwcm',
                'function'    => 'mbe_display_admin_page',// Uses the same callback function as parent menu.
            ),

            # Post Type :: View All Posts
            array(
                'parent_slug' => 'nwcm',
                'page_title'  => '',
                'menu_title'  => 'View News',
                'capability'  => '',
                'menu_slug'   => 'edit.php?post_type=nwcm_news',
                'function'    => null,// Doesn't need a callback function.
            ),

            # Post Type :: Add New Post
            array(
                'parent_slug' => 'nwcm',
                'page_title'  => '',
                'menu_title'  => 'Add News',
                'capability'  => '',
                'menu_slug'   => 'post-new.php?post_type=nwcm_news',
                'function'    => null,// Doesn't need a callback function.
            ),

            # Taxonomy :: Manage News Categories
            array(
                'parent_slug' => 'nwcm',
                'page_title'  => '',
                'menu_title'  => 'News Categories',
                'capability'  => '',
                'menu_slug'   => 'edit-tags.php?taxonomy=nwcm_news_category&post_type=nwcm_news',
                'function'    => null,// Doesn't need a callback function.
            ),

        );

        # Add each submenu item to custom admin menu.
        foreach ( $submenu_pages as $submenu ) {

            add_submenu_page(
                $submenu['parent_slug'],
                $submenu['page_title'],
                $submenu['menu_title'],
                $submenu['capability'],
                $submenu['menu_slug'],
                $submenu['function']
            );

        }

    }

    add_action( 'admin_menu', 'mbe_add_admin_menus', 1 );

    /* If you add any extra custom sub menu pages which are not a Custom Post Type or a Custom Taxonomy, you will need
     * to create a callback function for each of your custom submenu items you create above.
     */

    # Default Admin Page for Custom Admin Menu
    function mbe_display_admin_page() {

        # Display custom admin page content from newly added custom admin menu.
        echo '<div class="wrap">' . PHP_EOL;
        echo '<h2>My Custom Admin Page Title</h2>' . PHP_EOL;
        echo '<p>This is the custom admin page created from the custom admin menu.</p>' . PHP_EOL;
        echo '</div><!-- end .wrap -->' . PHP_EOL;
        echo '<div class="clear"></div>' . PHP_EOL;

    }

}

Hook into parent_file to correctly highlight your Custom Post Type and Custom Taxonomy submenu items with your custom parent menu/page.

if ( ! function_exists( 'mbe_set_current_menu' ) ) {

    function mbe_set_current_menu( $parent_file ) {
        global $submenu_file, $current_screen, $pagenow;

        # Set the submenu as active/current while anywhere in your Custom Post Type (nwcm_news)
        if ( $current_screen->post_type == 'nwcm_news' ) {

            if ( $pagenow == 'post.php' ) {
                $submenu_file = 'edit.php?post_type=' . $current_screen->post_type;
            }

            if ( $pagenow == 'edit-tags.php' ) {
                $submenu_file = 'edit-tags.php?taxonomy=nwcm_news_category&post_type=' . $current_screen->post_type;
            }

            $parent_file = 'nwcm';

        }

        return $parent_file;

    }

    add_filter( 'parent_file', 'mbe_set_current_menu' );

}

If you need any clarification about how any of this works, read the following pages from top to bottom.

  1. Adding Custom Parent Admin Menus
  2. Adding Custom Child Admin Menus
  3. Roles and Capabilities in WordPress
  4. Registering Custom Post Types
  5. Registering Custom Taxonomies
  6. WordPress Plugin API :: Action Reference
  7. WordPress Plugin API :: Action Reference :: init
  8. WordPress Plugin API :: Action Reference :: admin_menu
  9. WordPress Plugin API :: Filter Reference
  10. List of All WordPress Hooks (including actions and filters)
  • Unfortunately this is not what I need :( I already checked the codex and it doesn't talk about my case; Adding custom taxonomy menu to a menu created with "add_menu_page". The example you provided above doesn't use "add_menu_page" but just creates a new top level menu.. This is why I used 'show_in_menu' => 'nwcm'; I need my custom post type + associated txonomy to show under menu created using add_menu_page .. This a bit complex and I'm not sure if it is possible to achieve. – numediaweb Sep 5 '14 at 10:49
  • @numediaweb I tried forever to pass in a string to the argument show_in_menu for register_post_type(); like admin.php?page=nwcm ... But it just wouldn't work. However, with my solution, it allows a bit more flexibility and freedom with your WordPress administration development. You can mix in custom admin pages, custom Post Type pages, and custom Taxonomy pages. Peep the edit to my answer. :) – Michael Ecklund Sep 5 '14 at 16:36
  • Thanks Michael for the time spent on the answer :) Well, while the solution you provided will work but it has a drawback; you loose the WordPress "hook" or link between custom post type and the generated menu. This means when you click on for example on "Add News" it will take you to the correct page but the menu in left won't be highlighted because wordpress doesn't know the connection between custom post type "nwcm_news" and the "submenu_page" item.. I think what I'm trying to achieve is impossible.. will change my approach! thanks – numediaweb Sep 5 '14 at 19:05
  • @numediaweb The other option you have is to register your Custom Post Type, and custom Taxonomy. Attaching the two together under the same menu automatically. Then, you can add and custom admin pages as submenu items under your custom Post Type's menu. So basically, operating in reverse logic from what you're wanting to do. That would allow your submenu items to highlight correctly. – Michael Ecklund Sep 5 '14 at 20:02
  • @numediaweb I added a fix for correctly highlighting your Custom Post Type and Custom Taxonomy submenu items with your custom parent admin menu/page. – Michael Ecklund Sep 5 '14 at 20:21
2

Here is how i accomplished this: Inside of the admin_menu action, I added a submenu item to the parent menu item ('nwcm') with the URL parameter of the taxonomy edit page.

add_submenu_page('nwcm', 'News categories', 'News categories', 'edit_posts', 'edit-tags.php?taxonomy=nwcm_news&post_type=nwcm_news',false );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.