• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / 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|bool ) 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 245
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:
	 *  - Fall back (if one was specified), or bail.
 
[1] [2] [3] Next »

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 21st August 2020

Primary Sidebar

Information

Function name: wp_nav_menu
Plugin ref: WordPress
Version: 5.6.2
Sourcefile: wp-includes/nav-menu-template.php
File ref: wp-includes/nav-menu-template.php
Deprecated?: No
API Letters: M,N,W

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6.2
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites