Created
June 27, 2014 17:27
-
-
Save kalenjohnson/9fb75f48342d8537899a to your computer and use it in GitHub Desktop.
Walker Nav with Drawer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Cleaner walker for wp_nav_menu() | |
* | |
* Walker_Nav_Menu (WordPress default) example output: | |
* <li id="menu-item-8" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-8"><a href="/">Home</a></li> | |
* <li id="menu-item-9" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-9"><a href="/sample-page/">Sample Page</a></l | |
* | |
* Roots_Nav_Walker example output: | |
* <li class="menu-home"><a href="/">Home</a></li> | |
* <li class="menu-sample-page"><a href="/sample-page/">Sample Page</a></li> | |
*/ | |
class Roots_Nav_Walker extends Walker_Nav_Menu { | |
private $parent_ID; | |
private $locations; | |
private $menu; | |
private $menu_items; | |
function check_current($classes) { | |
return preg_match('/(current[-_])|active|dropdown/', $classes); | |
} | |
function start_lvl( &$output, $depth = 0, $args = array() ) { | |
$output .= "\n<ul class=\"dropdown-menu\"> \n<div class=\"container\"> \n<div class=\"row\">\n"; | |
} | |
function end_lvl( &$output, $depth = 0, $args = array() ) { | |
$output .= "\n</div> \n</div> \n</ul> \n"; | |
} | |
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) { | |
$item_html = ''; | |
parent::start_el($item_html, $item, $depth, $args); | |
if ($item->is_dropdown && ($depth === 0)) { | |
$this->parent_ID = $item->ID; | |
$item_html = str_replace('<a', '<a class="dropdown-toggle" data-toggle="dropdown" data-target="#"', $item_html); | |
} | |
elseif ($depth === 1) { | |
if ( ! $this->locations) | |
$this->locations = get_nav_menu_locations(); | |
if ( ! $this->menu) | |
$this->menu = wp_get_nav_menu_object( $this->locations['primary_navigation'] ); | |
if ( ! $this->menu_items ) | |
$this->menu_items = wp_get_nav_menu_items( $this->menu->term_id ); | |
$count = 0; | |
foreach ( $this->menu_items as $menu_item ) | |
if ( $menu_item->menu_item_parent == $this->parent_ID ) | |
$count ++; | |
$column = 12 / $count; | |
$item_html = str_replace('<li class="', "<li class=\"col-sm-{$column} ", $item_html); | |
$item_html = str_replace('<a', "<a class='title'", $item_html); | |
$hook_title = str_replace( ' ', '_', strtolower( $item->title ) ); | |
$item_html = apply_filters( "{$hook_title}_nav_menu", $item_html ); | |
} | |
elseif (stristr($item_html, 'li class="divider')) { | |
$item_html = preg_replace('/<a[^>]*>.*?<\/a>/iU', '', $item_html); | |
} | |
elseif (stristr($item_html, 'li class="dropdown-header')) { | |
$item_html = preg_replace('/<a[^>]*>(.*)<\/a>/iU', '$1', $item_html); | |
} | |
$item_html = apply_filters('roots/wp_nav_menu_item', $item_html); | |
$output .= $item_html; | |
} | |
function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) { | |
$element->is_dropdown = ((!empty($children_elements[$element->ID]) && (($depth + 1) < $max_depth || ($max_depth === 0)))); | |
if ($element->is_dropdown) { | |
$element->classes[] = 'dropdown'; | |
} | |
parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output); | |
} | |
} | |
/** | |
* Remove the id="" on nav menu items | |
* Return 'menu-slug' for nav menu classes | |
*/ | |
function roots_nav_menu_css_class($classes, $item) { | |
$slug = sanitize_title($item->title); | |
$classes = preg_replace('/(current(-menu-|[-_]page[-_])(item|parent|ancestor))/', 'active', $classes); | |
$classes = preg_replace('/^((menu|page)[-_\w+]+)+/', '', $classes); | |
$classes[] = 'menu-' . $slug; | |
$classes = array_unique($classes); | |
return array_filter($classes, 'is_element_empty'); | |
} | |
add_filter('nav_menu_css_class', 'roots_nav_menu_css_class', 10, 2); | |
add_filter('nav_menu_item_id', '__return_null'); | |
/** | |
* Clean up wp_nav_menu_args | |
* | |
* Remove the container | |
* Use Roots_Nav_Walker() by default | |
*/ | |
function roots_nav_menu_args($args = '') { | |
$roots_nav_menu_args['container'] = false; | |
if (!$args['items_wrap']) { | |
$roots_nav_menu_args['items_wrap'] = '<ul class="%2$s">%3$s</ul>'; | |
} | |
if (!$args['depth']) { | |
$roots_nav_menu_args['depth'] = 2; | |
} | |
// No Bootstrap dropdowns in footer | |
if (isset($args['menu']->slug) && $args['menu']->slug === 'footer') { | |
$args['walker'] = new Walker_Nav_Menu(); | |
} | |
if (!$args['walker']) { | |
$roots_nav_menu_args['walker'] = new Roots_Nav_Walker(); | |
} | |
return array_merge($args, $roots_nav_menu_args); | |
} | |
add_filter('wp_nav_menu_args', 'roots_nav_menu_args'); | |
add_filter( 'workouts_nav_menu', function ( $item_html ) { | |
global $competitor; | |
$term_title = 'train-tags'; | |
$featured_post = $competitor->featured; | |
ob_start(); | |
include dirname(__FILE__) . '/views/terms_widget.php'; | |
wp_reset_query(); | |
$competitor->featured->rewind_posts(); | |
return $item_html . ob_get_clean(); | |
}); | |
add_filter( 'fitness_tips_nav_menu', function ( $item_html ) { | |
global $competitor; | |
$widget_posts = $competitor->fitness_tips_nav; | |
ob_start(); | |
include dirname(__FILE__) . '/views/posts_widget.php'; | |
wp_reset_query(); | |
$competitor->fitness_tips_nav->rewind_posts(); | |
return $item_html . ob_get_clean(); | |
}); | |
add_filter( 'recipes_nav_menu', function ( $item_html ) { | |
global $competitor; | |
$term_title = 'recipe-tags'; | |
$featured_post = $competitor->recipes; | |
ob_start(); | |
include dirname(__FILE__) . '/views/terms_widget.php'; | |
wp_reset_query(); | |
$competitor->recipes->rewind_posts(); | |
return $item_html . ob_get_clean(); | |
}); | |
add_filter( 'healthy_eating_tips_nav_menu', function ( $item_html ) { | |
global $competitor; | |
$widget_posts = $competitor->health_tips_nav; | |
ob_start(); | |
include dirname(__FILE__) . '/views/posts_widget.php'; | |
wp_reset_query(); | |
$competitor->health_tips_nav->rewind_posts(); | |
return $item_html . ob_get_clean(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment