From bf412b07b1ee258995aebb65e7e2fd9a92f6826c Mon Sep 17 00:00:00 2001 From: Laury GvR Date: Tue, 6 Mar 2018 17:10:09 -0500 Subject: [PATCH] Fixed main nav being confused for top bar Since main nav and top bar use the same walker, the static var mainLevelCounter which keeps track of whether to apply drop-left classes was getting the wrong values. New walker created just for the main nav, separate from the top-bar walker. --- lib/menu-walker.php | 39 ++++++++++++++++++++++++++++++++++++++- lib/navigation.php | 2 +- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/menu-walker.php b/lib/menu-walker.php index cfe42e3..ae20467 100644 --- a/lib/menu-walker.php +++ b/lib/menu-walker.php @@ -4,6 +4,44 @@ */ 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 ) { + $element->has_children = ! empty( $children_elements[ $element->ID ] ); + $element->classes[] = ( $element->current || $element->current_item_ancestor ) ? 'active' : ''; + $element->classes[] = ( $element->has_children && 1 !== $max_depth ) ? 'has-dropdown' : ''; + parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); + } + + function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) { + $item_html = ''; + parent::start_el( $item_html, $object, $depth, $args ); + + // Insert style to display page's thumbnail + $item_style = ''; + if ($depth == 0 && has_post_thumbnail((int)$object->object_id)) { + $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id((int)$object->object_id), 'large'); + $item_style .= ""; + } + //$output .= ( 0 == $depth ) ? '
  • ' : ''; + $classes = empty( $object->classes ) ? array() : (array) $object->classes; + if ( in_array( 'label', $classes ) ) { + //$output .= '
  • '; + $item_html = preg_replace( '/]*>(.*)<\/a>/iU', '', $item_html ); + } + if ( in_array( 'divider', $classes ) ) { + $item_html = preg_replace( '/]*>( .* )<\/a>/iU', '', $item_html ); + } + if ($item_style) + $output .= $item_style; + $output .= $item_html; + } + + function start_lvl( &$output, $depth = 0, $args = array() ) { + $output .= "\n
      \n"; + } +} +endif; +if ( ! class_exists( 'Glm_Theme_Main_Nav_Walker' ) ) : +class Glm_Theme_Main_Nav_Walker extends Walker_Nav_Menu { function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) { static $mainLevelCounter; if ($depth == 0) { @@ -14,7 +52,6 @@ class Glm_Theme_Top_Bar_Walker extends Walker_Nav_Menu { $element->classes[] = ( $element->has_children && 1 !== $max_depth ) ? 'has-dropdown' : ''; $element->classes[] = ( $element->post_parent == 0 && $mainLevelCounter < 3 ) ? '' : "drop-left ctr-$mainLevelCounter"; parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); - $mainLevelCounter = 0; } function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) { diff --git a/lib/navigation.php b/lib/navigation.php index b69e414..84aacc1 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -30,7 +30,7 @@ if ( ! function_exists( 'glm_theme_top_bar' ) ) { 'link_after' => '', // after each link text 'depth' => 3, // limit the depth of the nav 'fallback_cb' => false, // fallback function (see below) - 'walker' => new Glm_Theme_Top_Bar_Walker() + 'walker' => new Glm_Theme_Main_Nav_Walker() )); } } -- 2.17.1