Show Custom Taxonomy Inside Custom Menu

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)

Leave a Comment