Manage remove [Tabs] from off canvas walker class
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 12 Nov 2015 17:23:36 +0000 (12:23 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 12 Nov 2015 17:23:36 +0000 (12:23 -0500)
Redo the off canvas menu class so it skips the generation of the level
if the title is [Tabs].

functions.php
lib/menu-walker.php
lib/navigation.php
parts/off-canvas-menu.php

index 3a9bf33..5877e6b 100644 (file)
@@ -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 '<ul'.(($class)?' class="'.$class.'"':'').'><!-- begin -->'."\n";
-    $elemCount = 1;
-    if ($navImg) {
-        echo '<li class="nav-img">' . $navImg . '</li>';
-    }
-    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 '<li class="has-dropdown';
-            if ($parent == 0 && $elemCount <= 2) {
-                echo ' drop-right';
-            } elseif ($parent == 0) {
-                echo ' drop-left';
-            }
-            echo '">'."\n";
-            echo '<a href="'.get_permalink($page->ID).'">'.$page->post_title.'</a>'."\n";
-            echo glm_page_menu($page->ID, 'sub-menu dropdown', true, $navImg);
-            echo '</li>'."\n";
-        } else {
-            echo '<li><a href="'.get_permalink($page->ID).'">'.$page->post_title.'</a></li>'."\n";
-        }
-        ++$elemCount;
-    }
-    echo '</ul><!-- end -->'."\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 '<div class="left-off-canvas-list"><ul class="home-off"><li class="page_item"><a href="'.home_url('/').'">Home</a></li></ul>';
-    wp_nav_menu(array(
-        'theme_location'  => 'top-bar',
-        'container'       => '',
-        'container_class' => '',
-        'menu'            => '',
-        'depth'           => 0,
-        'items_wrap'      => '<ul class="%2$s">%3$s</ul>',
-        'menu_class'      => 'nav-off',
-        'walker'          => new Emmetcounty_Off_Canvas_Walker()
-    ));
-    echo '</div>';
-    //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'          => '',
index 9cafa3a..0e27e80 100644 (file)
@@ -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 .= '<style>#menu-item-'.$object->ID.' > .dropdown::before {
-                    //border: 5px solid #fdefd4;
-                    //box-shadow: 1px 2px 9px -1px grey;
-                    //height: 140px;
-                    //width: 200px;
-                    //top:20px;
-                    //right:20px;
-                    //position:absolute;content:"";background-image: url("'.$thumbnail[0].'");</style>';
-            }
-            //$item_style .= '<li class="nav-img">' . $thumbnail[0] . '</li>';
-        }
-        //$output .= ( 0 == $depth ) ? '<li class="divider"></li>' : '';
         $classes = empty( $object->classes ) ? array() : (array) $object->classes;
         if ( in_array( 'label', $classes ) ) {
-            //$output .= '<li class="divider"></li>';
             $item_html = preg_replace( '/<a[^>]*>(.*)<\/a>/iU', '<label>$1</label>', $item_html );
         }
         if ( in_array( 'divider', $classes ) ) {
             $item_html = preg_replace( '/<a[^>]*>( .* )<\/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[^>]*>(.*)<\/a>/iU', '<label>$1</label>', $item_html );
+        }
+        if ( in_array( 'divider', $classes ) ) {
+            $item_html = preg_replace( '/<a[^>]*>( .* )<\/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<ul class=\"children\">\n";
+        if ( $this->currentItem != '[Tabs]'  ) {
+            $output .= "\n<ul class=\"children\">\n";
+        }
+    }
+    function end_el( &$output, $item, $depth = 0, $args = array() ) {
+        if ( $this->currentItem != '[Tabs]'  ) {
+            $output .= "</li>\n";
+        }
+    }
+    function end_lvl( &$output, $depth = 0, $args = array() ) {
+        if ( $this->currentItem != '[Tabs]'  ) {
+            $indent = str_repeat("\t", $depth);
+            $output .= "$indent</ul>\n";
+        }
     }
 }
 endif;
index 65b1ed7..cb374d7 100644 (file)
@@ -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 '<div class="left-off-canvas-list"><ul class="home-off"><li class="page_item"><a href="'.home_url('/').'">Home</a></li></ul>';
+    wp_nav_menu(array(
+        'theme_location'  => 'top-bar',
+        'container'       => '',
+        'container_class' => '',
+        'menu'            => '',
+        'depth'           => 0,
+        'items_wrap'      => '<ul class="%2$s">%3$s</ul>',
+        'menu_class'      => 'nav-off',
+        'walker'          => new Emmetcounty_Off_Canvas_Walker()
+    ));
+    echo '</div>';
+    //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 '<ul'.(($class)?' class="'.$class.'"':'').'><!-- begin -->'."\n";
+    $elemCount = 1;
+    if ($navImg) {
+        echo '<li class="nav-img">' . $navImg . '</li>';
+    }
+    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 '<li class="has-dropdown';
+            if ($parent == 0 && $elemCount <= 2) {
+                echo ' drop-right';
+            } elseif ($parent == 0) {
+                echo ' drop-left';
+            }
+            echo '">'."\n";
+            echo '<a href="'.get_permalink($page->ID).'">'.$page->post_title.'</a>'."\n";
+            echo glm_page_menu($page->ID, 'sub-menu dropdown', true, $navImg);
+            echo '</li>'."\n";
+        } else {
+            echo '<li><a href="'.get_permalink($page->ID).'">'.$page->post_title.'</a></li>'."\n";
+        }
+        ++$elemCount;
+    }
+    echo '</ul><!-- end -->'."\n";
 
+}
 /**
  * Footer
  */
index eef0767..b6b2c80 100644 (file)
@@ -1,6 +1,6 @@
 <aside class="left-off-canvas-menu hide-for-large-up">
-    <?php //glm_offcanvas_menu(); ?>
-    <?php glm_side_menu(true); ?>
+    <?php glm_offcanvas_menu(); ?>
+    <?php //glm_side_menu(true); ?>
     <ul class="offcavas-social-list">
         <li><a href="#"><img src="<?php echo get_template_directory_uri(); ?>/assets/facebook.png"></a></li>
         <li><a href="#"><img src="<?php echo get_template_directory_uri(); ?>/assets/twitter.png"></a></li>