Make WordPress Core

Changeset 15113


Ignore:
Timestamp:
06/02/2010 05:04:07 PM (14 years ago)
Author:
nacin
Message:

Add menu_id to wp_nav_menu() and move container_id to the container. Set the container arg to false to not use a container. fixes #13669

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/nav-menu-template.php

    r15105 r15113  
    111111 *
    112112 * menu - The menu that is desired.  Accepts (matching in order) id, slug, name. Defaults to blank.
    113  * menu_class - CSS class to use for the ul container of the menu list. Defaults to 'menu'.
     113 * menu_class - CSS class to use for the ul element which forms the menu. Defaults to 'menu'.
     114 * menu_id - The ID that is applied to the ul element which forms the menu. Defaults to the menu slug, incremented.
    114115 * container - Whether to wrap the ul, and what to wrap it with. Defaults to 'div'.
    115  * container_class - the class that is applied to the container. Defaults to blank.
    116  * container_id - The ID that is applied to the container.  Defaults to the menu slug, incremented.
     116 * container_class - the class that is applied to the container. Defaults to 'menu-{menu slug}-container'.
     117 * container_id - The ID that is applied to the container. Defaults to blank.
    117118 * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'.
    118119 * before - Text before the link text.
     
    130131 */
    131132function wp_nav_menu( $args = array() ) {
    132     global $_wp_nav_menu_slugs;
    133     $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu',
     133    static $menu_id_slugs = array();
     134
     135    $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
    134136    'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '',
    135137    'depth' => 0, 'walker' => '', 'theme_location' => '' );
     
    170172        return false;
    171173
    172     $nav_menu = '';
    173     $items = '';
    174     $container_allowedtags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
    175 
    176     if ( in_array( $args->container, $container_allowedtags ) ) {
    177         $class = $args->container_class ? ' class="' . esc_attr($args->container_class) . '"' : ' class="menu-'. $menu->slug .'-container"';
    178         $nav_menu .= '<'. $args->container . $class .'>';
     174    $nav_menu = $items = '';
     175
     176    $show_container = false;
     177    if ( $args->container ) {
     178        $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
     179        if ( in_array( $args->container, $allowed_tags ) ) {
     180            $show_container = true;
     181            $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
     182            $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
     183            $nav_menu .= '<'. $args->container . $id . $class . '>';
     184        }
    179185    }
    180186
     
    192198
    193199    // Attributes
    194     $slug = 'menu-' . $menu->slug;
    195     if ( ! is_array( $_wp_nav_menu_slugs ) )
    196         $_wp_nav_menu_slugs = array();
    197 
    198     while ( in_array( $slug, $_wp_nav_menu_slugs ) ) {
    199         if ( preg_match( '#-(\d+)$#', $slug, $matches ) )
    200             $slug = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $slug);
    201         else
    202             $slug = $slug . '-1';
    203     }
    204    
    205     $_wp_nav_menu_slugs[] = $slug;
    206        
    207     $attributes = ' id="' . ( empty( $args->container_id ) ? $slug : $args->container_id ) . '"';
     200    if ( ! empty( $args->menu_id ) ) {
     201        $slug = $args->menu_id;
     202    } else {
     203        $slug = 'menu-' . $menu->slug;
     204        while ( in_array( $slug, $menu_id_slugs ) ) {
     205            if ( preg_match( '#-(\d+)$#', $slug, $matches ) )
     206                $slug = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $slug);
     207            else
     208                $slug = $slug . '-1';
     209        }
     210    }
     211    $menu_id_slugs[] = $slug;
     212    $attributes = ' id="' . $slug . '"';
    208213    $attributes .= $args->menu_class ? ' class="'. $args->menu_class .'"' : '';
    209214
     
    218223    $nav_menu .= '</ul>';
    219224
    220     if ( in_array( $args->container, $container_allowedtags ) )
    221         $nav_menu .= '</'. $args->container .'>';
     225    if ( $show_container )
     226        $nav_menu .= '</' . $args->container . '>';
    222227
    223228    $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
Note: See TracChangeset for help on using the changeset viewer.