You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress:无需hack方法将自定义分类法添加到自定义后台菜单

How to Add Custom Taxonomies to a Custom WordPress Admin Menu (No Core Modifications Needed)

Great question! I’ve run into this exact issue before, so I know how frustrating it can be when WordPress’s default taxonomy settings feel restrictive. The good news is there are totally legitimate, core-compliant ways to get your custom taxonomy under a custom admin menu—no sketchy hacks or core code changes required. Let’s break down the two most reliable methods:

Method 1: Use the show_in_menu Parameter with Your Custom Menu Slug (Simplest Approach)

First, let’s clear up a common misconception: the show_in_menu argument for register_taxonomy() doesn’t only accept boolean values. In WordPress 4.4 and later, you can pass a string that matches the slug of your custom parent menu, and the taxonomy will automatically appear as a submenu item.

Here’s a step-by-step example:

  1. Register your custom admin menu with add_menu_page(), and make note of the menu slug (the fourth parameter):
    add_action('admin_menu', 'register_my_custom_parent_menu');
    function register_my_custom_parent_menu() {
        add_menu_page(
            'My Custom Dashboard', // Page title (shows in browser tab)
            'Custom Menu',         // Menu title (shows in admin sidebar)
            'manage_options',      // User capability required to access
            'my-custom-menu-slug', // Unique menu slug (this is critical)
            'my_custom_menu_callback', // Callback for the menu page content
            'dashicons-admin-tools', // Menu icon (use a Dashicon)
            20 // Position in the admin sidebar
        );
    }
    
  2. When registering your custom taxonomy, set show_in_menu to the slug of your custom parent menu:
    add_action('init', 'register_my_custom_taxonomy');
    function register_my_custom_taxonomy() {
        $taxonomy_args = array(
            'label' => 'Product Categories',
            'public' => true,
            'hierarchical' => true,
            'show_in_menu' => 'my-custom-menu-slug', // Match your custom menu slug here
            // Add other taxonomy settings as needed
        );
        register_taxonomy('product_category', 'custom_product', $taxonomy_args);
    }
    

That’s it! Your taxonomy’s management page will now nest directly under your custom admin menu, no extra work required.

Method 2: Manually Add the Taxonomy as a Submenu Item (Full Control)

If you need more control over the submenu order, or if you’re working with an older WordPress version where Method 1 isn’t supported, you can manually link the taxonomy’s admin page to your custom menu using add_submenu_page().

Here’s how to do it:

  1. First, set show_in_menu to false when registering your taxonomy to hide it from the default admin menu:
    add_action('init', 'register_my_custom_taxonomy');
    function register_my_custom_taxonomy() {
        $taxonomy_args = array(
            'label' => 'Product Tags',
            'public' => true,
            'show_in_menu' => false, // Hide from default menu location
            // Other taxonomy settings...
        );
        register_taxonomy('product_tag', 'custom_product', $taxonomy_args);
    }
    
  2. Use add_submenu_page() to add the taxonomy’s existing admin page as a submenu item under your custom menu:
    add_action('admin_menu', 'add_taxonomy_to_custom_menu');
    function add_taxonomy_to_custom_menu() {
        // Construct the URL to the taxonomy's management page
        $taxonomy_admin_url = admin_url('edit-tags.php?taxonomy=product_tag&post_type=custom_product');
        
        // Add it as a submenu to your custom parent menu
        add_submenu_page(
            'my-custom-menu-slug', // Parent menu slug
            'Manage Product Tags', // Page title
            'Product Tags',        // Submenu title
            'manage_options',      // Required capability
            $taxonomy_admin_url    // Use the taxonomy URL as the menu slug (links directly to the page)
        );
    }
    

This method lets you place the taxonomy submenu exactly where you want it, and it’s fully compliant with WordPress best practices—no "hack" required.

Why Your Initial "Hack" Might Be Unnecessary

The approach of creating a custom submenu page that redirects to the taxonomy page is often labeled a hack, but the two methods above are official, supported ways to achieve the same result cleanly. The key was realizing that show_in_menu accepts a menu slug string (not just true/false) in modern WordPress versions.


内容的提问来源于stack exchange,提问作者Orlando P.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:00:14