WordPress自定义分类层级识别异常求助:portfolio分类问题
Let's break down why your subcategory_2 is being treated as a top-level category instead of a child of main_category, and walk through practical fixes step by step:
1. Double-Check Subcategory Parent Assignment First
Start with the simplest check—head to your custom taxonomy's management page (under the Portfolio post type in WP Admin). Edit subcategory_2 and make sure its Parent dropdown is explicitly set to main_category. Save the changes and test publishing a new post with both selected. Sometimes a quick mis-save or overlooked dropdown selection is the root cause here.
2. Verify Custom Taxonomy Registration Parameters
Your custom taxonomy must have the hierarchical parameter set to true to support parent-child relationships. Double-check your registration code to confirm this critical setting:
register_taxonomy( 'your_custom_taxonomy', // Replace with your actual taxonomy slug 'portfolio', array( 'hierarchical' => true, // This enables hierarchy support 'labels' => array(/* Your label settings */), 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array('slug' => 'portfolio-category'), ) );
If hierarchical was set to false, WordPress treats the taxonomy like non-hierarchical tags, which explains the misclassification. Update the parameter, flush your permalinks (Settings → Permalinks → Save Changes), and retest.
3. Inspect Database Taxonomy Data (Backup First!)
If the parent setting looks correct in the admin but the issue persists, the problem might lie in your WordPress database. Access your site's database (via phpMyAdmin or similar) and check these two tables:
wp_terms: Locate theterm_idvalues forsubcategory_2andmain_category.wp_term_taxonomy: Find the row matchingsubcategory_2'sterm_id, then verify theparentfield matchesmain_category'sterm_id. If it's set to 0 or another incorrect value, update it (always backup your database before making manual edits!).
4. Rule Out Plugin/Theme Conflicts
Third-party plugins or custom theme code can sometimes interfere with the taxonomy selection UI. Test this by:
- Switching to a default WordPress theme (like Twenty Twenty-Four)
- Disabling all plugins except those used to register your custom post type/taxonomy
Test publishing a post withmain_categoryandsubcategory_2selected. If the issue disappears, re-enable plugins/themes one by one to identify the conflicting code.
5. Check for Special Characters in Subcategory Slug/Name
Rarely, special characters (like ampersands, accented characters, or unusual symbols) in the subcategory's slug or name can cause WordPress to misprocess its hierarchy. Try renaming subcategory_2 to a simple slug (e.g., subcategory-2) and updating its name to plain text, then retest.
内容的提问来源于stack exchange,提问作者Marek

