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

WordPress a2z

WordPress Dynamic API Reference

  • Home
  • Plugins
  • Themes
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / twentynineteen_add_dropdown_icons() – Add a dropdown icon to top-level menu items.

You appear to be a bot. Output may be restricted

Description

Add a dropdown icon to top-level menu items.

Usage

$string = twentynineteen_add_dropdown_icons( $item_output, $item, $depth, $args );

Parameters

$item_output
( string ) required – The menu item's starting HTML output.
$item
( WP_Post ) required – Menu item data object.
$depth
( int ) required – Depth of the menu. Used for padding.
$args
( stdClass ) required – An object of wp_nav_menu() arguments.

Returns

string Nav menu item start element.

Source

File name: twentynineteen/inc/icon-functions.php


Lines:

1 to 45 of 45
function twentynineteen_add_dropdown_icons( $item_output, $item, $depth, $args ) {

  // Only add class to 'top level' items on the 'primary' menu.
  if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
    return $item_output;
  }

  if ( in_array( 'mobile-parent-nav-menu-item', $item->classes, true ) && isset( $item->original_id ) ) {
    // Inject the keyboard_arrow_left SVG inside the parent nav menu item, and let the item link to the parent item.
    // @todo Only do this for nested submenus? If on a first-level submenu, then really the link could be "#" since the desire is to remove the target entirely.
    $link = sprintf(
      '<button class="menu-item-link-return" tabindex="-1">%s',
      twentynineteen_get_icon_svg( 'chevron_left', 24 )
    );

    // Replace opening <a> with <button>.
    $item_output = preg_replace(
      '/<a\s.*?>/',
      $link,
      $item_output,
      1 // Limit.
    );

    // Replace closing </a> with </button>.
    $item_output = preg_replace(
      '#</a>#i',
      '</button>',
      $item_output,
      1 // Limit.
    );

  } elseif ( in_array( 'menu-item-has-children', $item->classes, true ) ) {

    // Add SVG icon to parent items.
    $icon = twentynineteen_get_icon_svg( 'keyboard_arrow_down', 24 );

    $item_output .= sprintf(
      '<button class="submenu-expand" tabindex="-1">%s</button>',
      $icon
    );
  }

  return $item_output;
}
 

 View on GitHub View on Trac

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

Primary Sidebar

Information

Function name: twentynineteen_add_dropdown_icons
Plugin ref: Twenty Nineteen
Version: 2.3
Sourcefile: inc/icon-functions.php
File ref: inc/icon-functions.php
Deprecated?: No
API Letters: A,D,I,T

Footer

WordPress a2z
WordPress a2z
WordPress Dynamic API Reference
WordPress 6.0. PHP: 8.0.20
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:  wp-a2z.org
© Copyright WordPress a2z 2014-2022. All rights reserved.


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

  • Home
  • Blog
  • Sitemap
  • Sites