能否使用wp_get_nav_menu_items输出WordPress三级菜单项?
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_itemsto exclude deeper items (check for uses of thewp_get_nav_menu_itemsfilter 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 themenu_item_parentvalues 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

