Wordpress - how do I know if a menu item has children?

Wordpress - how do I know if a menu item has children?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Detecting Menu Children in WordPress Walkers

When building custom navigation structures in a WordPress theme, distinguishing between parent items and leaf nodes is a common requirement. You often need to apply specific CSS rules—like adding an arrow indicator or changing the cursor style—only for menu items that have submenus. The standard approach involves creating a custom Walker_Nav_Menu class to override the default output generation.

The core of this logic lies in the start_el method, which receives an $item object representing the current menu entry. To determine if this item has children, you must inspect the underlying data structure rather than relying on CSS classes that might not exist by default. The most reliable indicator is the presence of a child element within the $args->walker->menu_items collection.

Inspecting the Walker's Menu Items

Inside your custom walker class, specifically within the start_el method, you can access the full list of menu items associated with the current level via $args->walker->menu_items. This array contains every item processed by the walker for that specific menu location.

To check if the current $item has children, iterate through this array and look for any entry where the ID_PARENT property matches the ID of your current $item. If a match is found, it means there is at least one child node registered in the database or temporary walker state.

Here is how you can implement that logic:

class My_Walker_Nav_Menu extends Walker_Nav_Menu {
    function start_el(&$output, $item, $depth, $args) {
        // Check if there are any items in the walker's menu_items array
        if (!empty($args->walker->menu_items)) {
            foreach ($args->walker->menu_items as $child_item) {
                // Compare IDs to find children of the current item
                if ($child_item->ID == $item->ID && $child_item->ID_PARENT != 0) {
                    // This is a child, so we need to check who the parent is.
                    // However, a simpler check is often just seeing if any 
                    // item has this ID as its parent.
                    
                    // A more direct approach for "does this have children?"
                    // is checking if any item's ID_PARENT equals $item->ID.
                }
            }
        }
    }
}

The previous snippet was slightly convoluted because we need to check the ID_PARENT of the potential children, not the current item against itself. The corrected logic is simpler: iterate through $args->walker->menu_items and see if any object has its ID_PARENT equal to $item->ID.

class My_Walker_Nav_Menu extends Walker_Nav_Menu {
    function start_el(&$output, $item, $depth, $args) {
        $has_children = false;

        // Iterate through all items in the walker's list
        if (!empty($args->walker->menu_items)) {
            foreach ($args->walker->menu_items as $child_item) {
                // If any item has the current item's ID as its parent, it is a child
                if ($child_item->ID_PARENT == $item->ID) {
                    $has_children = true;
                    break;
                }
            }
        }

        // Apply logic based on whether children exist
        if ($has_children) {
            $output .= ' class="menu-item-has-children"';
        } else {
            $output .= ' class="menu-item-leaf"';
        }

        // Continue with standard output generation...
    }
}

Understanding the Data Flow

This approach works because wp_nav_menu passes a walker object that accumulates all items before rendering. Even though the HTML output is generated depth-first, the $args->walker->menu_items array holds the complete tree structure for that specific menu instance.

One potential pitfall with this method is performance on very large menus. Iterating through the entire list of items for every single element to check for children results in an O(n²) complexity in the worst case (though WordPress menus are rarely deep or wide enough to cause actual lag). If you find yourself dealing with thousands of menu items, consider caching the result or pre-calculating the hierarchy.

Another consideration is how you handle dynamic content. If your menu items are loaded via AJAX or constructed dynamically outside the standard wp_nav_menu context, the $args->walker->menu_items might not reflect the final state if the walker hasn't finished processing. However, within the standard start_el call, this array is populated correctly.

Alternative: Using ID_PARENT Directly

In some scenarios, specifically when dealing with deeply nested structures or custom database queries that bypass the standard Walker, you might not have access to $args->walker->menu_items. In those rare cases, WordPress does not automatically populate a children property on the $item object like it does for post queries. You are generally reliant on the ID_PARENT relationship stored in the wp_nav_menu_items table or the temporary walker state.

If you require this functionality in a context where the Walker array isn't accessible, you would need to query the wp_nav_menu_items database table directly using $wpdb. This involves joining against the menu locations and checking for rows where menu_item_parent equals the current item's ID.

global $wpdb;
$parent_id = $item->ID;
$sql = $wpdb->prepare("
    SELECT COUNT(*) as count 
    FROM {$wpdb->prefix}nav_menu_items 
    WHERE menu_item_parent = %d", $parent_id
);
$result = $wpdb->get_row($sql);

if ($result->count > 0) {
    // Has children
}

Direct database queries are heavier than checking the walker array, so they should be reserved for situations where the Walker data is insufficient or unavailable. For standard theme development, sticking to the $args->walker->menu_items inspection keeps your code lightweight and aligned with WordPress's internal architecture.

Our network