Fixed the top navigation mainlevel counter.
authorLaury GvR <laury@gaslightmedia.com>
Mon, 30 Apr 2018 20:57:16 +0000 (16:57 -0400)
committerLaury GvR <laury@gaslightmedia.com>
Mon, 30 Apr 2018 20:57:16 +0000 (16:57 -0400)
It was not resetting when a new menu was called due to the nature
of the static variable being used to keep track of how many main
level items have been iterated ($mainLevelCounter)

The solution was another static var ($topbar_id_old) comparing the
old topbar id to the current topbar id (derived from $args), and if
they differ reset the mainlevel counter.

This avoids the problem where the mainlevelcounter starts counting
up in the Top Links, causing the Drop-left class to be improperly
applied (since that looks at $mainLevelCounter < 3 but by the time
it gets there, if a Top Links menu exists, it's already at 2 or 3)

lib/menu-walker.php

index baefaee..813fa12 100644 (file)
@@ -5,8 +5,17 @@
 
 if ( ! class_exists( 'Glm_Theme_Top_Bar_Walker' ) ) :
 class Glm_Theme_Top_Bar_Walker extends Walker_Nav_Menu {
+    
     function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) {
         static $mainLevelCounter;
+        static $topbar_id_old;
+        $topbar_id = $args[0]->menu->term_id;
+        
+        if ($topbar_id_old !== $topbar_id) {
+            $topbar_id_old = $topbar_id;
+            $mainLevelCounter = 0;
+        }
+
         if ($depth == 0) {
             ++$mainLevelCounter;
         }
@@ -14,11 +23,10 @@ class Glm_Theme_Top_Bar_Walker extends Walker_Nav_Menu {
         $element->classes[] = ( $element->current || $element->current_item_ancestor ) ? 'active' : '';
         $element->classes[] = ( $element->has_children && 1 !== $max_depth ) ? 'has-dropdown' : '';
         $element->classes[] = ( $element->post_parent == 0 && $mainLevelCounter < 3 ) ? '' : 'drop-left';
-        parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
+        parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );    
     }
 
     function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {
-//      $topbar_id = $args->menu->term_id;
         $item_html = '';
         parent::start_el( $item_html, $object, $depth, $args );