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

Daniel Pringle

Web Design

  • About
  • Portfolio
  • Code
  • Blog
  • Contact
Home › Code › Custom menus with WordPress Walkers

Custom menus with WordPress Walkers

August 3, 2021 by Dan

function dpuk_add_sub_menu_toggle2( $output, $item, $depth=0, $args=[], $id=0 ) {

	$arrow = '<i class="dropdown dashicons dashicons-arrow-down"></i>';

	if ( '#' === $item->url ) {

        $item_output = $args->before;
        $item_output .= '<span>'. $item->title;
        $item_output .= '</span>';
        $item_output .= $args->after;

		if ( 0 === $depth && in_array( 'menu-item-has-children', $item->classes, true ) ) {

			// Add toggle button.
			$item_output .= '<button class="sub-menu-toggle" aria-expanded="false" onClick="expandSubMenu(this)">';
			$item_output .= $arrow;
			$item_output .= '<span class="screen-reader-text">' . esc_html__( 'Open menu', 'nachala' ) . '</span>';
			$item_output .= '</button>';
		}
		return $item_output;

	} else {
		if ( 0 === $depth && in_array( 'menu-item-has-children', $item->classes, true ) ) {

			// Add toggle button.
			$output .= '<button class="sub-menu-toggle" aria-expanded="false" onClick="expandSubMenu(this)">';
			$output .= $arrow;
			$output .= '<span class="screen-reader-text">' . esc_html__( 'Open menu', 'nachala' ) . '</span>';
			$output .= '</button>';
		}
		return $output;
	}

}
add_filter( 'walker_nav_menu_start_el', '

dpuk_sub_menu_toggle2', 10, 4 );
Filed under: WordPress

Primary Sidebar

Working with businesses to develop their online identity building scale-able web solutions that work for you, your team and your customers.

  • * Web Design & Development *
  • * SEO *
  • * Content Marketing *

Recent Posts

  • What is web design?
  • How to Move from http to https with WordPress
  • Is your website GDPR compliant?
  • Why your business needs a website

Footer

Quick Links

  • About
  • Portfolio
  • Web Design & Development
  • SEO
  • Digital Marketing
  • Code

say hello

  • Facebook
  • GitHub
  • LinkedIn
  • Pinterest
  • Twitter

Recent Posts

  • What is web design?
  • How to Move from http to https with WordPress
  • Is your website GDPR compliant?
  • Why your business needs a website

© Copyright 2016 - 2022 danielpringle.co.uk · All Rights Reserved ·