wp_nav_menu() – Displays a navigation menu.

You appear to be a bot. Output may be restricted

Description

Displays a navigation menu.

Usage

$void|string|false = wp_nav_menu( $args );

Parameters

$args
( array ) optional – { Optional. Array of nav menu arguments.
$menu
( int|string|WP_Term ) optional – Desired menu. Accepts a menu ID, slug, name, or object. Default empty.
$menu_class
( string ) optional – CSS class to use for the ul element which forms the menu. Default 'menu'.
$menu_id
( string ) optional – The ID that is applied to the ul element which forms the menu. Default is the menu slug, incremented.
$container
( string ) optional – Whether to wrap the ul, and what to wrap it with. Default 'div'.
$container_class
( string ) optional – Class that is applied to the container. Default 'menu-{menu slug}-container'.
$container_id
( string ) optional – The ID that is applied to the container. Default empty.
$container_aria_label
( string ) optional – The aria-label attribute that is applied to the container when it's a nav element. Default empty.
$fallback_cb
( callable|false ) optional – If the menu doesn't exist, a callback function will fire. Default is 'wp_page_menu'. Set to false for no fallback.
$before
( string ) optional – Text before the link markup. Default empty.
$after
( string ) optional – Text after the link markup. Default empty.
$link_before
( string ) optional – Text before the link text. Default empty.
$link_after
( string ) optional – Text after the link text. Default empty.
$echo
( bool ) optional – Whether to echo the menu or return it. Default true.
$depth
( int ) optional – How many levels of the hierarchy are to be included.

  1. means all. Default 0.
  2. Default 0.

$walker
( object ) optional – Instance of a custom walker class. Default empty.
$theme_location
( string ) optional – Theme location to be used. Must be registered with register_nav_menu() in order to be selectable by the user.
$items_wrap
( string ) optional – How the list items should be wrapped. Uses printf() format with numbered placeholders. Default is a ul with an id and class.
$item_spacing
( string ) optional – Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'. Default 'preserve'. }

Returns

void|string|false Void if 'echo' argument is true, menu output if 'echo' is false. False if there are no items or no menu was found.

Source

File name: wordpress/wp-includes/nav-menu-template.php


Lines:

1 to 100 of 267
function wp_nav_menu( $args = array() ) {
  static $menu_id_slugs = array();

  $defaults = array(
    'menu'                 => '',
    'container'            => 'div',
    'container_class'      => '',
    'container_id'         => '',
    'container_aria_label' => '',
    'menu_class'           => 'menu',
    'menu_id'              => '',
    'echo'                 => true,
    'fallback_cb'          => 'wp_page_menu',
    'before'               => '',
    'after'                => '',
    'link_before'          => '',
    'link_after'           => '',
    'items_wrap'           => '<ul id="%1$s" class="%2$s">%3$s</ul>',
    'item_spacing'         => 'preserve',
    'depth'                => 0,
    'walker'               => '',
    'theme_location'       => '',
  );

  $args = wp_parse_args( $args, $defaults );

  if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ), true ) ) {
    // Invalid value, fall back to default.
    $args['item_spacing'] = $defaults['item_spacing'];
  }

  
/**
 * Filters the arguments used to display a navigation menu.
 *
 * @since 3.0.0
 *
 * @see wp_nav_menu()
 *
 * @param array $args Array of wp_nav_menu() arguments.
 */
  $args = apply_filters( 'wp_nav_menu_args', $args );
  $args = (object) $args;

  
/**
 * Filters whether to short-circuit the wp_nav_menu() output.
 *
 * Returning a non-null value from the filter will short-circuit wp_nav_menu(),
 * echoing that value if $args->echo is true, returning that value otherwise.
 *
 * @since 3.9.0
 *
 * @see wp_nav_menu()
 *
 * @param string|null $output Nav menu output to short-circuit with. Default null.
 * @param stdClass    $args   An object containing wp_nav_menu() arguments.
 */
  $nav_menu = apply_filters( 'pre_wp_nav_menu', null, $args );

  if ( null !== $nav_menu ) {
    if ( $args->echo ) {
      echo $nav_menu;
      return;
    }

    return $nav_menu;
  }

  // Get the nav menu based on the requested menu.
  $menu = wp_get_nav_menu_object( $args->menu );

  // Get the nav menu based on the theme_location.
  $locations = get_nav_menu_locations();
  if ( ! $menu && $args->theme_location && $locations && isset( $locations[ $args->theme_location ] ) ) {
    $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
  }

  // Get the first menu that has items if we still can't find a menu.
  if ( ! $menu && ! $args->theme_location ) {
    $menus = wp_get_nav_menus();
    foreach ( $menus as $menu_maybe ) {
      $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) );
      if ( $menu_items ) {
        $menu = $menu_maybe;
        break;
      }
    }
  }

  if ( empty( $args->menu ) ) {
    $args->menu = $menu;
  }

  // If the menu exists, get its items.
  if ( $menu && ! is_wp_error( $menu ) && ! isset( $menu_items ) ) {
    $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
  }

  /*
	 * If no menu was found:

 View on GitHub View on Trac