I’ve used the menu walker below to display dynamic submenus with great effect in the past.

It displays 2nd or lower-level menu items if the current page is a parent, sibling, or descendant of the page.

For example, if the menu hierarchy is:

A
B
1
i
ii

It will show, 1, i, and ii if on pages B, 1, i, or ii. What I want to do is, add the parent item (B in the example) to that so that the top-level parent also appears.

I’ve read up on walkers, tried to make sense of this code, but am still stuck even on where to begin.

class Walkah extends Walker_Nav_Menu {

   public $tree_type = array( 'post_type', 'taxonomy', 'custom' );

   public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );

   public $stat = false;

   public function start_lvl( &$output, $depth = 0, $args = array() ) {
      $indent = str_repeat("t", $depth);
      $output .= "n$indent<ul class="menu">";
      global $stat;
      $this->stat = true;
   }

   public function end_lvl( &$output, $depth = 0, $args = array() ) {
      $indent = str_repeat("t", $depth);
      $output .= "$indent</ul>n";
      global $stat;
      $this->stat = false;
   }

   public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
       $indent = ( $depth ) ? str_repeat( "t", $depth ) : '';

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

       $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
       $class_names = $class_names ? ' class="item ' . esc_attr( $class_names ) . '"' : '';

       $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 .'>';*/
       if ( $args->walker->has_children ){
            $output .= $indent . '<li class="ui dropdown item">';
        }
        else
        {
            global $stat;

            if($this->stat==true){
                $output .= $indent . '';
            }
            else{
                $output .= $indent . '';
            }

        }

       $atts = array();
       $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
       $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
       $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
       $atts['href']   = ! empty( $item->url )        ? $item->url        : '';

       $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );

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

       $icon = '';
       $no_link = '';
       if ( $args->walker->has_children ){
            $icon = "<i class="dropdown icon"></i>";
            $item_output = $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after . $icon;
       }
       else
       {
           $class_names = 'class="ui teal item"';

           $item_output = $args->before;
           $item_output .= '<a'. $attributes .''. $class_names .'>';
           $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
           $item_output .= '</a>' . $icon;
           $item_output .= $args->after;
        }

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );

   }

   public function end_el( &$output, $item, $depth = 0, $args = array() ) {
      $output .= "</li>n";
   }

}

Read more here: Create sub menu 3 level with walker class


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: