From ba63d206aeb0cfff8847316d72c6139a36a7215c Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Thu, 12 Nov 2015 12:23:36 -0500 Subject: [PATCH] Manage remove [Tabs] from off canvas walker class Redo the off canvas menu class so it skips the generation of the level if the title is [Tabs]. --- functions.php | 82 -------------------------- lib/menu-walker.php | 120 ++++++++++++++++++++++++++++++-------- lib/navigation.php | 80 ++++++++++++++++++++++++- parts/off-canvas-menu.php | 4 +- 4 files changed, 176 insertions(+), 110 deletions(-) diff --git a/functions.php b/functions.php index 3a9bf33..5877e6b 100644 --- a/functions.php +++ b/functions.php @@ -43,61 +43,6 @@ function glm_get_menu_options() return $menu_options; } -/** - * glm_page_menu - * - * Grab the top level pages and their sub pages as the main navigation - */ -function glm_page_menu($parent = 0, $class = '', $subnav = false, $navImg = null) -{ - $menuConfig = glm_get_menu_options(); - $frontPageId = get_option('page_on_front'); - $parents = array(); - $args = array( - 'post_type' => 'page', - 'parent' => $parent, - 'number' => '', - 'exclude' => $frontPageId, - 'post_status' => 'publish', - 'sort_order' => 'asc', - 'sort_column' => 'menu_order' - ); - if ($parent == 0 && $menuConfig['menu_options']['main_level_pages']) { - $args['include'] = $menuConfig['menu_options']['main_level_pages']; - } - $pages = get_pages($args); - echo ''."\n"; - $elemCount = 1; - if ($navImg) { - echo ''; - } - foreach ($pages as $page) { - if ($parent == 0) { - $navImg = get_the_post_thumbnail($page->ID); - } else { - $navImg = null; - } - $childs = get_pages('child_of=' . $page->ID); - if (count($childs) > 0) { - echo '
  • '."\n"; - echo ''.$page->post_title.''."\n"; - echo glm_page_menu($page->ID, 'sub-menu dropdown', true, $navImg); - echo '
  • '."\n"; - } else { - echo '
  • '.$page->post_title.'
  • '."\n"; - } - ++$elemCount; - } - echo ''."\n"; - -} - function SearchFilter($query) { if(isset($_GET['searchType'])) { $searchType = $_GET['searchType']; @@ -108,33 +53,6 @@ function SearchFilter($query) { return $query; } add_filter('pre_get_posts','SearchFilter'); - -/** - * glm_offcanvas_menu - * - * Generate the list of pages as nested ul li list - */ -function glm_offcanvas_menu() -{ - echo '
    '; - wp_nav_menu(array( - 'theme_location' => 'top-bar', - 'container' => '', - 'container_class' => '', - 'menu' => '', - 'depth' => 0, - 'items_wrap' => '
      %3$s
    ', - 'menu_class' => 'nav-off', - 'walker' => new Emmetcounty_Off_Canvas_Walker() - )); - echo '
    '; - //wp_page_menu(array( - //'depth' => 0, - //'sort_column' => 'menu_order', - //'menu_class' => 'left-off-canvas-list' - //)); -} - add_theme_support('post-thumbnails'); $defaults = array( 'default-color' => '', diff --git a/lib/menu-walker.php b/lib/menu-walker.php index 9cafa3a..0e27e80 100644 --- a/lib/menu-walker.php +++ b/lib/menu-walker.php @@ -21,31 +21,16 @@ class Emmetcounty_Top_Bar_Walker extends Walker_Nav_Menu { // 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)); - if ($thumbnail[0]) { - //$item_style .= ''; - } - //$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) + if ( $item_style ) { $output .= $item_style; + } $output .= $item_html; } @@ -61,16 +46,101 @@ class Emmetcounty_Top_Bar_Walker extends Walker_Nav_Menu { endif; if ( ! class_exists( 'Emmetcounty_Off_Canvas_Walker' ) ) : class Emmetcounty_Off_Canvas_Walker extends Walker_Nav_Menu { - function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) { - static $topLevelCounter = 0; - $element->has_children = ! empty( $children_elements[ $element->ID ] ); - $element->classes[] = ( $element->post_parent == 0 && $element->has_children && 1 !== $max_depth ) ? 'page_item_has_children' : ''; - //$element->classes[] = 'menu-order-' . $element->menu_order; - $element->classes[] = 'page_item'; - parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); + public $currentItem; + //function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) { + //static $topLevelCounter = 0; + //$element->has_children = ! empty( $children_elements[ $element->ID ] ); + //$element->classes[] = ( $element->post_parent == 0 && $element->has_children && 1 !== $max_depth ) ? 'page_item_has_children' : ''; + //$element->classes[] = 'page_item'; + //parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); + //} + public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { + if ( ! $element ) { + return; + } + + $id_field = $this->db_fields['id']; + $id = $element->$id_field; + $isTabs = ($element->title == '[Tabs]'); + + //display this element + $this->has_children = ! empty( $children_elements[ $id ] ); + if ( isset( $args[0] ) && is_array( $args[0] ) ) { + $args[0]['has_children'] = $this->has_children; // Backwards compatibility. + } + if (!$isTabs) { + $element->classes[] = ( $element->post_parent == 0 && $this->has_children && 1 !== $max_depth ) ? 'page_item_has_children' : ''; + $element->classes[] = 'page_item'; + + $cb_args = array_merge( array(&$output, $element, $depth), $args); + call_user_func_array(array($this, 'start_el'), $cb_args); + } + + // descend only when the depth is right and there are childrens for this element + if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) { + + foreach( $children_elements[ $id ] as $child ){ + + if ( !isset($newlevel) && !$isTabs) { + $newlevel = true; + //start the child delimiter + $cb_args = array_merge( array(&$output, $depth), $args); + call_user_func_array(array($this, 'start_lvl'), $cb_args); + } + + $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output ); + } + unset( $children_elements[ $id ] ); + } + + if ( isset($newlevel) && $newlevel && !$isTabs){ + //end the child delimiter + $cb_args = array_merge( array(&$output, $depth), $args); + call_user_func_array(array($this, 'end_lvl'), $cb_args); + } + + //end this element + if (!$isTabs) { + $cb_args = array_merge( array(&$output, $element, $depth), $args); + call_user_func_array(array($this, 'end_el'), $cb_args); + } + } + function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) { + $this->currentItem = $object; + $item_html = ''; + parent::start_el( $item_html, $object, $depth, $args ); + + // Insert style to display page's thumbnail + $item_style = ''; + $classes = empty( $object->classes ) ? array() : (array) $object->classes; + if ( in_array( 'label', $classes ) ) { + $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; + } + if ( $object->title != '[Tabs]' ) { + $output .= $item_html; + } } function start_lvl( &$output, $depth = 0, $args = array() ) { - $output .= "\n
      \n"; + if ( $this->currentItem != '[Tabs]' ) { + $output .= "\n
        \n"; + } + } + function end_el( &$output, $item, $depth = 0, $args = array() ) { + if ( $this->currentItem != '[Tabs]' ) { + $output .= "\n"; + } + } + function end_lvl( &$output, $depth = 0, $args = array() ) { + if ( $this->currentItem != '[Tabs]' ) { + $indent = str_repeat("\t", $depth); + $output .= "$indent
      \n"; + } } } endif; diff --git a/lib/navigation.php b/lib/navigation.php index 65b1ed7..cb374d7 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -44,11 +44,89 @@ if ( ! function_exists( 'emmetcounty_mobile_off_canvas' ) ) { 'link_after' => '', // after each link text 'depth' => 5, // limit the depth of the nav 'fallback_cb' => false, // fallback function (see below) - 'walker' => new Emmetcounty_Offcanvas_Walker() + 'walker' => new Emmetcounty_Off_Canvas_Walker() )); } } +/** + * glm_offcanvas_menu + * + * Generate the list of pages as nested ul li list + */ +function glm_offcanvas_menu() +{ + echo '
      '; + wp_nav_menu(array( + 'theme_location' => 'top-bar', + 'container' => '', + 'container_class' => '', + 'menu' => '', + 'depth' => 0, + 'items_wrap' => '
        %3$s
      ', + 'menu_class' => 'nav-off', + 'walker' => new Emmetcounty_Off_Canvas_Walker() + )); + echo '
      '; + //wp_page_menu(array( + //'depth' => 0, + //'sort_column' => 'menu_order', + //'menu_class' => 'left-off-canvas-list' + //)); +} +/** + * glm_page_menu + * + * Grab the top level pages and their sub pages as the main navigation + */ +function glm_page_menu($parent = 0, $class = '', $subnav = false, $navImg = null) +{ + $menuConfig = glm_get_menu_options(); + $frontPageId = get_option('page_on_front'); + $parents = array(); + $args = array( + 'post_type' => 'page', + 'parent' => $parent, + 'number' => '', + 'exclude' => $frontPageId, + 'post_status' => 'publish', + 'sort_order' => 'asc', + 'sort_column' => 'menu_order' + ); + if ($parent == 0 && $menuConfig['menu_options']['main_level_pages']) { + $args['include'] = $menuConfig['menu_options']['main_level_pages']; + } + $pages = get_pages($args); + echo ''."\n"; + $elemCount = 1; + if ($navImg) { + echo ''; + } + foreach ($pages as $page) { + if ($parent == 0) { + $navImg = get_the_post_thumbnail($page->ID); + } else { + $navImg = null; + } + $childs = get_pages('child_of=' . $page->ID); + if (count($childs) > 0) { + echo '
    • '."\n"; + echo ''.$page->post_title.''."\n"; + echo glm_page_menu($page->ID, 'sub-menu dropdown', true, $navImg); + echo '
    • '."\n"; + } else { + echo '
    • '.$page->post_title.'
    • '."\n"; + } + ++$elemCount; + } + echo '
    '."\n"; +} /** * Footer */ diff --git a/parts/off-canvas-menu.php b/parts/off-canvas-menu.php index eef0767..b6b2c80 100644 --- a/parts/off-canvas-menu.php +++ b/parts/off-canvas-menu.php @@ -1,6 +1,6 @@