Walker_Nav_Menu::start_el( string $output, WP_Post $item, int $depth, stdClass $args = null, int $id )

Starts the element output.


Description Description

See also See also


Top ↑

Parameters Parameters

$output

(string) (Required) Used to append additional content (passed by reference).

$item

(WP_Post) (Required) Menu item data object.

$depth

(int) (Required) Depth of menu item. Used for padding.

$args

(stdClass) (Optional) An object of wp_nav_menu() arguments.

Default value: null

$id

(int) (Required) Current item ID.


Top ↑

Source Source

File: wp-includes/class-walker-nav-menu.php

	public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
			$t = '';
			$n = '';
		} else {
			$t = "\t";
			$n = "\n";
		}
		$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';

		$classes   = empty( $item->classes ) ? array() : (array) $item->classes;
		$classes[] = 'menu-item-' . $item->ID;

		/**
		 * Filters the arguments for a single nav menu item.
		 *
		 * @since 4.4.0
		 *
		 * @param stdClass $args  An object of wp_nav_menu() arguments.
		 * @param WP_Post  $item  Menu item data object.
		 * @param int      $depth Depth of menu item. Used for padding.
		 */
		$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );

		/**
		 * Filters the CSS classes applied to a menu item's list item element.
		 *
		 * @since 3.0.0
		 * @since 4.1.0 The `$depth` parameter was added.
		 *
		 * @param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element.
		 * @param WP_Post  $item    The current menu item.
		 * @param stdClass $args    An object of wp_nav_menu() arguments.
		 * @param int      $depth   Depth of menu item. Used for padding.
		 */
		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

		/**
		 * Filters the ID applied to a menu item's list item element.
		 *
		 * @since 3.0.1
		 * @since 4.1.0 The `$depth` parameter was added.
		 *
		 * @param string   $menu_id The ID that is applied to the menu item's `<li>` element.
		 * @param WP_Post  $item    The current menu item.
		 * @param stdClass $args    An object of wp_nav_menu() arguments.
		 * @param int      $depth   Depth of menu item. Used for padding.
		 */
		$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth );
		$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

		$output .= $indent . '<li' . $id . $class_names . '>';

		$atts           = array();
		$atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
		$atts['target'] = ! empty( $item->target ) ? $item->target : '';
		if ( '_blank' === $item->target && empty( $item->xfn ) ) {
			$atts['rel'] = 'noopener noreferrer';
		} else {
			$atts['rel'] = $item->xfn;
		}
		$atts['href']         = ! empty( $item->url ) ? $item->url : '';
		$atts['aria-current'] = $item->current ? 'page' : '';

		/**
		 * Filters the HTML attributes applied to a menu item's anchor element.
		 *
		 * @since 3.6.0
		 * @since 4.1.0 The `$depth` parameter was added.
		 *
		 * @param array $atts {
		 *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
		 *
		 *     @type string $title        Title attribute.
		 *     @type string $target       Target attribute.
		 *     @type string $rel          The rel attribute.
		 *     @type string $href         The href attribute.
		 *     @type string $aria_current The aria-current attribute.
		 * }
		 * @param WP_Post  $item  The current menu item.
		 * @param stdClass $args  An object of wp_nav_menu() arguments.
		 * @param int      $depth Depth of menu item. Used for padding.
		 */
		$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );

		$attributes = '';
		foreach ( $atts as $attr => $value ) {
			if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
				$value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
				$attributes .= ' ' . $attr . '="' . $value . '"';
			}
		}

		/** This filter is documented in wp-includes/post-template.php */
		$title = apply_filters( 'the_title', $item->title, $item->ID );

		/**
		 * Filters a menu item's title.
		 *
		 * @since 4.4.0
		 *
		 * @param string   $title The menu item's title.
		 * @param WP_Post  $item  The current menu item.
		 * @param stdClass $args  An object of wp_nav_menu() arguments.
		 * @param int      $depth Depth of menu item. Used for padding.
		 */
		$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );

		$item_output  = $args->before;
		$item_output .= '<a' . $attributes . '>';
		$item_output .= $args->link_before . $title . $args->link_after;
		$item_output .= '</a>';
		$item_output .= $args->after;

		/**
		 * Filters a menu item's starting output.
		 *
		 * The menu item's starting output only includes `$args->before`, the opening `<a>`,
		 * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
		 * no filter for modifying the opening and closing `<li>` for a menu item.
		 *
		 * @since 3.0.0
		 *
		 * @param string   $item_output The menu item's starting HTML output.
		 * @param WP_Post  $item        Menu item data object.
		 * @param int      $depth       Depth of menu item. Used for padding.
		 * @param stdClass $args        An object of wp_nav_menu() arguments.
		 */
		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
	}


Top ↑

Changelog Changelog

Changelog
Version Description
4.4.0 The 'nav_menu_item_args' filter was added.
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by abdelrahman96

    Found that and i think it may be useful
    The variable $item is an object that represents the an item of the menu. This is an example of a custom link:

    object(WP_Post)#497 (40) {
      ["ID"]=>
      int(160)
      ["post_author"]=>
      string(1) "1"
      ["post_date"]=>
      string(19) "2016-03-26 17:59:07"
      ["post_date_gmt"]=>
      string(19) "2016-03-26 17:59:07"
      ["post_content"]=>
      string(15) "Home Sweet Home"
      ["post_title"]=>
      string(4) "Home"
      ["post_excerpt"]=>
      string(0) ""
      ["post_status"]=>
      string(7) "publish"
      ["comment_status"]=>
      string(6) "closed"
      ["ping_status"]=>
      string(6) "closed"
      ["post_password"]=>
      string(0) ""
      ["post_name"]=>
      string(4) "home"
      ["to_ping"]=>
      string(0) ""
      ["pinged"]=>
      string(0) ""
      ["post_modified"]=>
      string(19) "2016-08-31 23:53:19"
      ["post_modified_gmt"]=>
      string(19) "2016-08-31 21:53:19"
      ["post_content_filtered"]=>
      string(0) ""
      ["post_parent"]=>
      int(0)
      ["guid"]=>
      string(33) "http://localhost/wordpress/?p=160"
      ["menu_order"]=>
      int(1)
      ["post_type"]=>
      string(13) "nav_menu_item"
      ["post_mime_type"]=>
      string(0) ""
      ["comment_count"]=>
      string(1) "0"
      ["filter"]=>
      string(3) "raw"
      ["db_id"]=>
      int(160)
      ["menu_item_parent"]=>
      string(1) "0"
      ["object_id"]=>
      string(3) "160"
      ["object"]=>
      string(6) "custom"
      ["type"]=>
      string(6) "custom"
      ["type_label"]=>
      string(11) "Custom Link"
      ["title"]=>
      string(4) "Home"
      ["url"]=>
      string(27) "http://localhost/wordpress/"
      ["target"]=>
      string(0) ""
      ["attr_title"]=>
      string(0) ""
      ["description"]=>
      string(15) "Home Sweet Home"
      ["classes"]=>
      array(7) {
        [0]=>
        string(0) ""
        [1]=>
        string(9) "menu-item"
        [2]=>
        string(21) "menu-item-type-custom"
        [3]=>
        string(23) "menu-item-object-custom"
        [4]=>
        string(17) "current-menu-item"
        [5]=>
        string(17) "current_page_item"
        [6]=>
        string(14) "menu-item-home"
      }
      ["xfn"]=>
      string(0) ""
      ["current"]=>
      bool(true)
      ["current_item_ancestor"]=>
      bool(false)
      ["current_item_parent"]=>
      bool(false)
    }

    The variable $args contains every information we pass into the function wp_nav_menu. This can be used to enter some information from the arguments into our WordPress custom menu.

    object(stdClass)#486 (16) {
      ["menu"]=>
      object(WP_Term)#489 (10) {
        ["term_id"]=>
        int(6)
        ["name"]=>
        string(12) "Primary Menu"
        ["slug"]=>
        string(12) "primary-menu"
        ["term_group"]=>
        int(0)
        ["term_taxonomy_id"]=>
        int(6)
        ["taxonomy"]=>
        string(8) "nav_menu"
        ["description"]=>
        string(0) ""
        ["parent"]=>
        int(0)
        ["count"]=>
        int(7)
        ["filter"]=>
        string(3) "raw"
      }
      ["container"]=>
      string(3) "div"
      ["container_class"]=>
      string(0) ""
      ["container_id"]=>
      string(0) ""
      ["menu_class"]=>
      string(12) "primary-menu"
      ["menu_id"]=>
      string(0) ""
      ["echo"]=>
      bool(true)
      ["fallback_cb"]=>
      string(12) "wp_page_menu"
      ["before"]=>
      string(0) ""
      ["after"]=>
      string(0) ""
      ["link_before"]=>
      string(0) ""
      ["link_after"]=>
      string(0) ""
      ["items_wrap"]=>
      string(36) "<ul>%3$s</ul>"
      ["depth"]=>
      int(0)
      ["walker"]=>
      object(IBenic_Walker)#484 (4) {
        ["tree_type"]=>
        array(3) {
          [0]=>
          string(9) "post_type"
          [1]=>
          string(8) "taxonomy"
          [2]=>
          string(6) "custom"
        }
        ["db_fields"]=>
        array(2) {
          ["parent"]=>
          string(16) "menu_item_parent"
          ["id"]=>
          string(5) "db_id"
        }
        ["max_pages"]=>
        int(1)
        ["has_children"]=>
        bool(false)
      }
      ["theme_location"]=>
      string(7) "primary"
    }

You must log in before being able to contribute a note or feedback.