• Hello, I’m building my own theme with a custom nav menu which extends the walker_nav_menu.

    I use the code below in my functions.php. All it does is remove the standard id’s and classes. And add a ‘dropdown arrow’ icon for each submenu.

    How can I add the parent name as an id to the dropdown ul?

    add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
        add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);
        add_filter('page_css_class', 'my_css_attributes_filter', 100, 1);
        function my_css_attributes_filter($var) {
          return is_array($var) ? array_intersect($var, array('current-menu-item')) : '';
        }
    
        class custom_menu extends Walker_Nav_Menu {
    	function start_lvl(&$output, $depth) {
            $output .= '<i class="fa fa-angle-down" aria-hidden="true"></i><ul id="'.$args->parent_id.'">';
            }
            function end_lvl(&$output, $depth) {
                $output .= '</ul>';
            }
        }

    Hope someone can help me, thanks.

    – Thierry

Viewing 1 replies (of 1 total)
  • samanthagrimm

    (@samanthagrimm)

    Hi,

    I was having the same issue. After looking around a bunch, this is what I came up with that works: You need to create a private variable outside your functions. I used the item’s ID instead of name.

    
    class My_Walker_Nav_Menu extends Walker_Nav_Menu {
    		private $curItemID;
    function start_lvl(&$output, $depth=0, $args = array() ) {
    			 $itemID = $this->curItemID;
    	     $indent = str_repeat("\t", $depth);
    	     $output .= "\n$indent<ul id=\"dropdown".esc_attr($itemID)."\" class=\"sub-menu\" >\n";
    	   }
    
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
    	  {
    	    global $wp_query, $wpdb;
    			$this->curItemID = $item->ID;
    ...

    I hope this helps!

Viewing 1 replies (of 1 total)
  • The topic ‘Add name of parent li to submenu ul as id (walker_nav_menu)’ is closed to new replies.