WordPress.org

Make WordPress Core

Ticket #13793: patch_menu_child_of.patch

File patch_menu_child_of.patch, 2.1 KB (added by DreadLox, 8 years ago)

Patch to add a child_of parameter to wp_name_menu

  • wp-includes/nav-menu-template.php

     
    134134
    135135        $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
    136136        'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '',
    137         'depth' => 0, 'walker' => '', 'theme_location' => '' );
     137        'depth' => 0, 'walker' => '', 'theme_location' => '', 'child_of'=>0 );
    138138
    139139        $args = wp_parse_args( $args, $defaults );
    140140        $args = apply_filters( 'wp_nav_menu_args', $args );
     
    187187        // Set up the $menu_item variables
    188188        _wp_menu_item_classes_by_context( $menu_items );
    189189
     190        if($args->child_of) $menu_items = & _wp_nav_menu_item_children( $args->child_of, $menu_items );
     191   
    190192        $sorted_menu_items = array();
    191193        foreach ( (array) $menu_items as $key => $menu_item )
    192194                $sorted_menu_items[$menu_item->menu_order] = $menu_item;
     
    357359}
    358360
    359361/**
     362 * Retrieve child menu items from list of menus matching menu ID.
     363 *
     364 * Matches against the menus parameter against the page ID. Also matches all
     365 * children for the same to retrieve all children of a menu. Does not make any
     366 * SQL queries to get the children.
     367 *
     368 * @since 1.5.1
     369 *
     370 * @param int $menu_id Menu Item ID.
     371 * @param array $items List of menu items' objects.
     372 * @return array
     373 */
     374function &_wp_nav_menu_item_children($item_id, $items) {
     375   
     376    $item_list = array();
     377    foreach ( (array) $items as $item ) {
     378        if ( $item->menu_item_parent == $item_id ) {
     379            $item_list[] = $item;
     380           
     381            $children = _wp_nav_menu_item_children($item->db_id, $items);
     382            if ( $children ) {
     383                $item_list = array_merge($item_list, $children);
     384            }
     385        }
     386    }
     387   
     388    return $item_list;
     389}
     390
     391/**
    360392 * Retrieve the HTML list content for nav menu items.
    361393 *
    362394 * @uses Walker_Nav_Menu to create HTML list content.