• I’ve got a walker that counts the number of the class “break” appear in $depth = 1 unordered lists. I need to add this number as a class to each unordered lists parent <li>, but by the time I’ve counted the number of breaks in the child unordered list, the parent list item has already been generated, so I can’t add the class. I’m’ so close to finishing this walker, I’ve got to be missing something really small. Please help!

    // mega menu walker
    /*
    	ONE REMAINING BUG:
    	- Need to add class to LI containing mega-menu-columns-#
    */
    class megaMenuWalker extends Walker_Nav_Menu {
    	private $column_limit = 3; /* needs to be set for each site */
        private $show_widget = false;
    	private $column_count = 0;
        static $li_count = 0;
        function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
            $classes = empty($item->classes) ? array() : (array) $item->classes;
            $item_id = $item->ID;
            if ($depth == 0) self::$li_count = 0;
            if ($depth == 0 && in_array("widget", $classes)) {
                $this->show_widget = true;
    			$this->column_count++;
            }
    		if ($depth == 1 && self::$li_count == 1) {
    			$this->column_count++;
    		}
            if ($depth == 1 && in_array("break", $classes) && self::$li_count != 1 && $this->column_count < $this->column_limit) {
                $output .= "</ul><ul class=\"sub-menu\">";
    			$this->column_count++;
            }
    		if ($depth == 0 && $this->column_count > 0) {
    			// always returns 0 unless there's a widget
    			$mega_menu_class = " mega-menu-columns-" . $this->column_count;
    		}
            $class_names = join(" ", apply_filters("nav_menu_css_class", array_filter($classes), $item));
            $class_names = " class=\"" . esc_attr($class_names . $mega_menu_class) . "\"";
            $output .= sprintf(
                "<li id=\"menu-item-%s\"%s><a href=\"%s\">%s</a>",
                $item_id,
                $class_names,
                $item->url,
                $item->title
            );
            self::$li_count++;
        }
        function start_lvl(&$output, $depth = 0, $args = array()) {
            if ($depth == 0) {
    			$output .= "<section>";
            }
            $output .= "<ul class=\"sub-menu\">";
        }
        function end_lvl(&$output, $depth = 0, $args = array()) {
            $output .= "</ul>";
            if ($depth == 0) {
                if ($this->show_widget) {
                    ob_start();
                    dynamic_sidebar("Navigation Callout");
                    $widget = ob_get_contents();
                    ob_end_clean();
                    $output .= $widget;
                    $this->show_widget = false;
                }
                $output .= "</section>";
            }
        }
        function end_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
    		if ($depth == 0 && $this->column_count > 0) {
    			/* needs to be added to opening level 0 li */
    			$column_count_class = " mega-menu-columns-" . $this->column_count;
    			$output .= $column_count_class;
    			/* end */
    			$this->column_count = 0;
    		}
            $output .= "</li>";
        }
    }
  • The topic ‘Walker to count number of child classes and add number to parent classes’ is closed to new replies.