Make WordPress Core


Ignore:
Timestamp:
04/07/2010 03:25:48 PM (15 years ago)
Author:
ryan
Message:

Nav menu fixes. Props ptahdunbar. see #12896 fixes #12844

File:
1 edited

Legend:

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

    r14013 r14031  
    2323 * link_after - Text after the link.
    2424 * echo - Whether to echo the menu or return it. Defaults to echo.
    25  * show_home - If you set this argument, then it will display the link to the home page. The show_home argument really just needs to be set to the value of the text of the link.
     25 *
     26 * @todo show_home - If you set this argument, then it will display the link to the home page. The show_home argument really just needs to be set to the value of the text of the link.
    2627 *
    2728 * @since 3.0.0
     
    3233    $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'menu_class' => 'menu', 'echo' => true,
    3334    'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '',
    34     'depth' => 0, 'walker' => '' );
     35    'depth' => 0, 'walker' => '', 'context' => 'frontend' );
    3536
    3637    $args = wp_parse_args( $args, $defaults );
     
    5253        }
    5354    }
    54 
    55     if ( $menu && ! is_wp_error( $menu ) )
    56         $args->menu = $menu->term_id;
    57     $nav_menu = '';
    58 
    59     if ( 'div' == $args->container ) {
    60         $class = $args->container_class ? ' class="' . esc_attr($args->container_class) . '"' : '';
    61 
    62         if ( is_nav_menu($menu) ) {
    63             $nav_menu .= '<div id="menu-' . $menu->slug . '"'. $class .'>';
    64         } else {
    65             $nav_menu .= '<div'. $class .'>';
    66         }
    67     }
    68 
    69     $nav_menu .= wp_get_nav_menu( $args );
    70 
    71     if ( 'div' == $args->container )
    72         $nav_menu .= '</div>';
    73 
    74     $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
    75 
    76     if ( $args->echo )
    77         echo $nav_menu;
    78     else
    79         return $nav_menu;
    80 }
    81 
    82 /**
    83  * Returns a Navigation Menu.
    84  *
    85  * See wp_nav_menu() for args.
    86  *
    87  * @since 3.0.0
    88  *
    89  * @param array $args Arguments
    90  * @return mixed $output False if menu doesn't exists, else, returns the menu.
    91  **/
    92 function wp_get_nav_menu( $args = array() ) {
    93     $defaults = array( 'menu' => '', 'menu_class' => 'menu', 'context' => 'frontend', 'depth' => 0,
    94     'fallback_cb' => '', 'walker' => '', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', );
    95 
    96     $args = wp_parse_args( $args, $defaults );
    97     $args = apply_filters( 'wp_get_nav_menu_args', $args );
    98     $args = (object) $args;
    99 
    100     // Variable setup
    101     $nav_menu = '';
    102     $items = '';
    103 
    104     // Get the menu object
    105     $menu = wp_get_nav_menu_object( $args->menu );
    106 
     55   
    10756    // If the menu exists, get it's items.
    10857    if ( $menu && !is_wp_error($menu) )
     
    11160    // If no menu was found or if the menu has no items, call the fallback_cb
    11261    if ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) ) ) {
    113         if ( function_exists($args->fallback_cb) || is_callable( $args->fallback_cb ) ) {
    114             $_args = array_merge( (array) $args, array('echo' => false) );
    115             return call_user_func( $args->fallback_cb, $_args );
     62        if ( 'frontend' == $args->context && ( function_exists($args->fallback_cb) || is_callable( $args->fallback_cb ) ) ) {
     63            return call_user_func( $args->fallback_cb, (array) $args );
    11664        }
     65    }
     66   
     67    $nav_menu = '';
     68    $items = '';
     69    $container_allowedtags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'p', 'nav' ) );
     70
     71    if ( in_array( $args->container, $container_allowedtags ) ) {
     72        $class = $args->container_class ? ' class="' . esc_attr($args->container_class) . '"' : ' class="menu-'. $menu->slug .'-container"';
     73        $nav_menu .= '<'. $args->container . $class .'>';
    11774    }
    11875
     
    12380    $items .= walk_nav_menu_tree( $menu_items, $args->depth, $args );
    12481
    125     // CSS class
    126     $ul_class = $args->menu_class ? ' class="'. $args->menu_class .'"' : '';
    127     $nav_menu .= '<ul'. $ul_class .'>';
     82    // Attributes   
     83    $attributes  = ' id="menu-' . $menu->slug . '"';
     84    $attributes .= $args->menu_class ? ' class="'. $args->menu_class .'"' : '';
     85   
     86    $nav_menu .= '<ul'. $attributes .'>';
    12887
    12988    // Allow plugins to hook into the menu to add their own <li>'s
     
    13897    $nav_menu .= '</ul>';
    13998
    140     return apply_filters( 'wp_get_nav_menu', $nav_menu );
     99    if ( in_array( $args->container, $container_allowedtags ) )
     100        $nav_menu .= '</'. $args->container .'>';
     101
     102    $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
     103
     104    if ( $args->echo )
     105        echo $nav_menu;
     106    else
     107        return $nav_menu;
    141108}
    142109
     
    162129            $output .= $args->before;
    163130            $output .= '<a'. $attributes .'>';
    164             $output .= $args->link_before . apply_filters('the_title', $menu_item->title) . $args->link_after;
     131            $output .= $args->link_before . apply_filters( 'the_title', $menu_item->title ) . $args->link_after;
    165132            $output .= '</a>';
    166133            $output .= $args->after;
     
    219186    }
    220187
    221     return $output;
     188    return apply_filters( 'wp_get_nav_menu_item', $output, $context, $args );
    222189}
    223190?>
Note: See TracChangeset for help on using the changeset viewer.