• 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_Widget::widget() – Outputs the content for the current Navigation Menu widget instance.

You appear to be a bot. Output may be restricted

Description

Outputs the content for the current Navigation Menu widget instance.

Usage

WP_Nav_Menu_Widget::widget( $args, $instance );

Parameters

$args
( array ) required – Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
$instance
( array ) required – Settings for the current Navigation Menu widget instance.

Returns

void

Source

File name: wordpress/wp-includes/widgets/class-wp-nav-menu-widget.php
Lines:

1 to 71 of 71
  public function widget( $args, $instance ) {
    // Get menu.
    $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;

    if ( ! $nav_menu ) {
      return;
    }

    $default_title = __( 'Menu' );
    $title         = ! empty( $instance['title'] ) ? $instance['title'] : '';

    
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
    $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );

    echo $args['before_widget'];

    if ( $title ) {
      echo $args['before_title'] . $title . $args['after_title'];
    }

    $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';

    
/**
 * Filters the HTML format of widgets with navigation links.
 *
 * @since 5.5.0
 *
 * @param string $format The type of markup to use in widgets with navigation links.
 *                       Accepts 'html5', 'xhtml'.
 */
    $format = apply_filters( 'navigation_widgets_format', $format );

    if ( 'html5' === $format ) {
      // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
      $title      = trim( strip_tags( $title ) );
      $aria_label = $title ? $title : $default_title;

      $nav_menu_args = array(
        'fallback_cb'          => '',
        'menu'                 => $nav_menu,
        'container'            => 'nav',
        'container_aria_label' => $aria_label,
        'items_wrap'           => '<ul id="%1$s" class="%2$s">%3$s</ul>',
      );
    } else {
      $nav_menu_args = array(
        'fallback_cb' => '',
        'menu'        => $nav_menu,
      );
    }

    
/**
 * Filters the arguments for the Navigation Menu widget.
 *
 * @since 4.2.0
 * @since 4.4.0 Added the `$instance` parameter.
 *
 * @param array   $nav_menu_args {
 *     An array of arguments passed to wp_nav_menu() to retrieve a navigation menu.
 *
 *     @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty.
 *     @type mixed         $menu        Menu ID, slug, or name.
 * }
 * @param WP_Term $nav_menu      Nav menu object for the current menu.
 * @param array   $args          Display arguments for the current widget.
 * @param array   $instance      Array of settings for the current widget.
 */
    wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );

    echo $args['after_widget'];
  }
 

 View on GitHub View on Trac

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

Primary Sidebar

Information

Function name: WP_Nav_Menu_Widget::widget
Class ref: WP_Nav_Menu_Widget
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/widgets/class-wp-nav-menu-widget.php
File ref: wp-includes/widgets/class-wp-nav-menu-widget.php
Deprecated?: No
API Letters: M,N,W

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
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