在自定义菜单中显示自定义分类

时间:2014-09-04 作者:numediaweb

我有一个自定义菜单,使用add_menu_page:

add_menu_page(\'My menu\' , \'Some text\', \'read\', \'nwcm\');
在它下面,我显示了一个自定义的post类型菜单项;

// 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\'
    ) ,
));
然后,我添加了一个与“nwcm\\U news”帖子类型挂钩的自定义分类法:

// 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
));
父菜单和自定义帖子类型都正确显示。。。但是,分类菜单没有显示:(

我如何解决这个问题?我查过了this solution 但答案缺少完整的代码示例。。

2 个回复
最合适的回答,由SO网友:Michael Ecklund 整理而成

你有一些乱码。

我已经将您的代码重新格式化为实际有效的代码。

以下解决方案允许您为自定义Post Type菜单提供所需的菜单名称。只需更改标签“menu\\u 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
));
我不能百分之百确定您是想在自己的自定义管理菜单下添加内容,还是只想更改自定义帖子类型的菜单名称。

我在menu_name “NWCM”到labels 自定义帖子类型。

我强烈建议您通读并完全理解注册自定义帖子类型和分类的参数和参数。


EDIT: 09/05/2014

如果你想完全添加你自己的自定义管理菜单,并混入你自己的自定义帖子类型、自定义分类法和任何其他自定义管理页面。。。以下解决方案有效。请注意,这只是一个起点,你不必百分之百地做到“t”。这只是一个例子。。。我建议您修改它,以便您或您的开发人员能够理解和维护它。

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\' );

}
如果您需要任何关于这些工作原理的说明,请从头到尾阅读以下页面。

  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)

SO网友:NRTRX

我是这样完成的:在admin\\u菜单操作中,我向父菜单项(“nwcm”)添加了一个子菜单项,并将menu\\u slug参数设置为分类法编辑页面的URL。

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

结束

相关推荐

Fix Admin Dashboard Posts tab

我有WordPress v3。5.1和使用WPZoom中的主题。我添加了一个名为WordPress SEO的SEO插件,它又在我的帖子管理表中添加了4列,这导致这些列挤在一起,现在很难看到所有帖子。我曾尝试在管理CSS中更改CSS,但这根本没有帮助,当我使用Firebug更改它时,它会有所帮助。如何修复压扁的立柱?为了更好地理解,您可以在以下位置查看图像副本:http://justawebbie.com/facebook/post-window.jpg.