'', 'container' => 'div', 'container_class' => '', 'menu_class' => 'menu', 'echo' => true,
'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '',
'depth' => 0, 'walker' => '', 'context' => 'frontend' );
$args = wp_parse_args( $args, $defaults );
$args = apply_filters( 'wp_nav_menu_args', $args );
$args = (object) $args;
// Get the nav menu
$menu = wp_get_nav_menu_object( $args->menu );
// If we couldn't find a menu based off the name, id or slug,
// get the first menu that has items.
if ( !$menu ) {
$menus = wp_get_nav_menus();
foreach ( $menus as $menu_maybe ) {
if ( wp_get_nav_menu_items($menu_maybe->term_id) ) {
$menu = $menu_maybe;
break;
}
}
}
// If the menu exists, get it's items.
if ( $menu && !is_wp_error($menu) )
$menu_items = wp_get_nav_menu_items( $menu->term_id, $args->context );
// If no menu was found or if the menu has no items, call the fallback_cb
if ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) ) ) {
if ( 'frontend' == $args->context && ( function_exists($args->fallback_cb) || is_callable( $args->fallback_cb ) ) ) {
return call_user_func( $args->fallback_cb, (array) $args );
}
}
$nav_menu = '';
$items = '';
$container_allowedtags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'p', 'nav' ) );
if ( in_array( $args->container, $container_allowedtags ) ) {
$class = $args->container_class ? ' class="' . esc_attr($args->container_class) . '"' : ' class="menu-'. $menu->slug .'-container"';
$nav_menu .= '<'. $args->container . $class .'>';
}
// Set up the $menu_item variables
foreach ( (array) $menu_items as $key => $menu_item )
$menu_items[$menu_item->menu_order] = wp_setup_nav_menu_item( $menu_item, 'frontend' );
$items .= walk_nav_menu_tree( $menu_items, $args->depth, $args );
// Attributes
$attributes = ' id="menu-' . $menu->slug . '"';
$attributes .= $args->menu_class ? ' class="'. $args->menu_class .'"' : '';
$nav_menu .= '
';
// Allow plugins to hook into the menu to add their own - 's
if ( 'frontend' == $args->context ) {
$items = apply_filters( 'wp_nav_menu_items', $items, $args );
$items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
$nav_menu .= $items;
} else {
$nav_menu .= $items;
}
$nav_menu .= '
';
if ( in_array( $args->container, $container_allowedtags ) )
$nav_menu .= ''. $args->container .'>';
$nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
if ( $args->echo )
echo $nav_menu;
else
return $nav_menu;
}
/**
* Returns the menu item formatted based on it's context.
*
* @since 3.0.0
*
* @param string $menu_item The menu item to format.
* @param string $context The context to which the menu item will be formatted to.
* @param string $args Optional. Args used for the 'template' context.
* @return string $output The menu formatted menu item.
*/
function wp_get_nav_menu_item( $menu_item, $context = 'frontend', $args = array() ) {
$output = '';
switch ( $context ) {
case 'frontend':
$attributes = ! empty( $menu_item->attr_title ) ? ' title="' . esc_attr( $menu_item->attr_title ) .'"' : '';
$attributes .= ! empty( $menu_item->target ) ? ' target="' . esc_attr( $menu_item->target ) .'"' : '';
$attributes .= ! empty( $menu_item->xfn ) ? ' rel="' . esc_attr( $menu_item->xfn ) .'"' : '';
$attributes .= ! empty( $menu_item->url ) ? ' href="' . esc_attr( $menu_item->url ) .'"' : '';
$output .= $args->before;
$output .= '';
$output .= $args->link_before . apply_filters( 'the_title', $menu_item->title ) . $args->link_after;
$output .= '';
$output .= $args->after;
break;
case 'backend':
$output .= '- ';
$output .= ''. esc_html( $menu_item->title ) .'';
$output .= '';
$output .= ''. esc_html( $menu_item->append ) .'';
// Actions
$output .= ' | ';
$output .= '';
$output .= '
';
// Menu Item Settings
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
break;
case 'custom':
case 'taxonomy':
case 'post_type':
$output .= '';
// Menu item hidden fields
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
break;
}
return apply_filters( 'wp_get_nav_menu_item', $output, $context, $args );
}
?>