Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RevConcept/ed21aa867fd36e7f199724606c7a8c7e to your computer and use it in GitHub Desktop.
Save RevConcept/ed21aa867fd36e7f199724606c7a8c7e to your computer and use it in GitHub Desktop.
An improvement on my original gist that splits the WordPress navigation into two menus (for sites with a centered logo for example). This version accounts for sites that are also using dropdown menus. It will only take the top level menu items into account when calculating the breakpoint location.
/* ==========================================================================
Custom Walker: Split menu based on item count
========================================================================== */
class mbt_split_nav_walker extends Walker_Nav_Menu {
var $current_menu = null;
var $break_point = null;
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
// Get the locations of nav menus
$theme_locations = get_nav_menu_locations();
// Get the menu object of the current nav menu based on the returned theme location
$menu_obj = get_term( $theme_locations[$args->theme_location], 'nav_menu' );
// Get sub menu/child counts
$menu_items = wp_get_nav_menu_items($menu_obj->term_id);
$menu_item_parents = array_map(function($o) { return $o->menu_item_parent; }, $menu_items);
$child_count = array_count_values($menu_item_parents);
// Remove menus with no submenu form array
unset($child_count[0]);
// add remaining menu counts for total sub menu count
$child_count = array_sum($child_count);
// Get parent menu item sequential counts
$menu_counter = 0;
global $menu_counter;
if($depth == 0) {
$menu_counter++;
}
// set current menu object
if( !isset( $this->current_menu ) ) {
$this->current_menu = wp_get_nav_menu_object( $menu_obj->term_id );
}
// set breakpoint
if( !isset( $this->break_point ) ) {
$divisor = $this->current_menu->count - $child_count;
$this->break_point = ceil( $divisor / 2 ) + 1;
}
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
if( ($this->break_point == $menu_counter) && ($depth == 0) ) {
$output .= $indent . '</li></span></ul><ul class="nav top-nav cf right"><span><li' . $id . $value . $class_names .'>';
} else {
$output .= $indent . '<li' . $id . $value . $class_names . '>';
}
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
//$item_output .= $menu_counter;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment