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

能否使用wp_get_nav_menu_items输出WordPress三级菜单项?

Does wp_get_nav_menu_items() support third-level menu items?

Absolutely, the wp_get_nav_menu_items() function does retrieve third-level (and even deeper) menu items—this isn't a limitation of the core WordPress function itself. The issue you're hitting is almost certainly how the mega-menu script you're adapting is processing and structuring the returned items.

Why most examples only show two levels

A lot of basic menu scripts stop at two levels because they only handle direct parent-child relationships (top-level items → their immediate children) without adding logic to recursively check for deeper nested items. The script you're using might be filtering the menu items to only include top-level and second-level entries, or it's not building out a hierarchical structure that accounts for third-level items.

How to fix this

First, double-check that your menu in the WordPress admin actually has third-level items set up correctly (i.e., you've dragged menu items under second-level items to nest them). Once you confirm that, you'll need to adjust the script's menu processing logic to handle deeper hierarchies.

Here's a simple recursive function you can use to restructure the flat array returned by wp_get_nav_menu_items() into a nested hierarchy that includes all levels:

function build_nested_menu($menu_items, $parent_id = 0) {
    $nested_menu = array();
    
    foreach ($menu_items as $item) {
        // Check if this item is a child of the current parent
        if ((int)$item->menu_item_parent === $parent_id) {
            // Recursively find children of this item
            $child_items = build_nested_menu($menu_items, $item->ID);
            
            // Add children to the item if there are any
            if (!empty($child_items)) {
                $item->children = $child_items;
            }
            
            $nested_menu[] = $item;
        }
    }
    
    return $nested_menu;
}

// Usage with your menu:
$menu_slug = 'your-menu-slug'; // Replace with your menu's slug/name/ID
$flat_menu_items = wp_get_nav_menu_items($menu_slug);
$hierarchical_menu = build_nested_menu($flat_menu_items);

Then, in your mega-menu template code, you'll need to loop through the hierarchical structure recursively. For example:

function render_mega_menu($menu_items) {
    echo '<ul>';
    foreach ($menu_items as $item) {
        echo '<li><a href="' . esc_url($item->url) . '">' . esc_html($item->title) . '</a>';
        
        // If this item has children (including third-level), render them
        if (isset($item->children)) {
            render_mega_menu($item->children);
        }
        
        echo '</li>';
    }
    echo '</ul>';
}

// Render the full menu
render_mega_menu($hierarchical_menu);

Things to check if it still doesn't work

  • Make sure no other plugins or theme functions are filtering wp_get_nav_menu_items to exclude deeper items (check for uses of the wp_get_nav_menu_items filter hook).
  • Verify that the third-level menu items in your admin have the correct parent ID set (you can debug this by var_dump($flat_menu_items) to see the menu_item_parent values for each item).
  • Check if the original mega-menu script has hardcoded limits (e.g., loops that only run twice for top and second levels) that you need to remove or replace with recursive logic.

内容的提问来源于stack exchange,提问作者Connie

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:51:16