public
Created — forked from awshout/foundation4-topbar-menu.php

WordPress Walker for ZURB's Foundation Top Bar

  • Download Gist
foundation-menu.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
add_theme_support('menus');
 
/*
http://codex.wordpress.org/Function_Reference/register_nav_menus#Examples
*/
register_nav_menus(array(
'top-bar-l' => 'Left Top Bar', // registers the menu in the WordPress admin menu editor
'top-bar-r' => 'Right Top Bar'
));
 
// the left top bar
function foundation_top_bar_l() {
wp_nav_menu(array(
'container' => false, // remove nav container
'container_class' => 'menu', // class of container
'menu' => '', // menu name
'menu_class' => 'top-bar-menu left', // adding custom nav class
'theme_location' => 'top-bar-l', // where it's located in the theme
'before' => '', // before each link <a>
'after' => '', // after each link </a>
'link_before' => '', // before each link text
'link_after' => '', // after each link text
'depth' => 5, // limit the depth of the nav
'fallback_cb' => false, // fallback function (see below)
'walker' => new top_bar_walker()
));
} // end left top bar
 
// the right top bar
function foundation_top_bar_r() {
wp_nav_menu(array(
'container' => false, // remove nav container
'container_class' => '', // class of container
'menu' => '', // menu name
'menu_class' => 'top-bar-menu right', // adding custom nav class
'theme_location' => 'top-bar-r', // where it's located in the theme
'before' => '', // before each link <a>
'after' => '', // after each link </a>
'link_before' => '', // before each link text
'link_after' => '', // after each link text
'depth' => 5, // limit the depth of the nav
'fallback_cb' => false, // fallback function (see below)
'walker' => new top_bar_walker()
));
} // end right top bar
foundation-nav-walker.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/*
Customize the output of menus for Foundation top bar classes and add descriptions
 
http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output
http://code.hyperspatial.com/1514/twitter-bootstrap-walker-classes/
*/
class top_bar_walker extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ($depth) ? str_repeat("\t", $depth) : '';
 
$class_names = $value = '';
 
$classes = empty($item->classes) ? array() : (array) $item->classes;
$classes[] = ($item->current) ? 'active' : '';
$classes[] = ($args->has_children) ? 'has-dropdown' : '';
$args->link_before = (in_array('section', $classes)) ? '<label>' : '';
$args->link_after = (in_array('section', $classes)) ? '</label>' : '';
$output .= (in_array('section', $classes)) ? '<li class="divider"></li>' : '';
 
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args ) );
$class_names = strlen(trim($class_names)) > 0 ? ' class="'.esc_attr($class_names).'"' : '';
$output .= ($depth == 0) ? $indent.'<li class="divider"></li>' : '';
$output .= $indent.'<li id="menu-item-'.$item->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 .= (!in_array('section', $classes)) ? '<a'.$attributes.'>' : '';
$item_output .= $args->link_before.apply_filters('the_title', $item->title, $item->ID);
$item_output .= $args->link_after;
$item_output .= (!in_array('section', $classes)) ? '</a>' : '';
$item_output .= $args->after;
 
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
function end_el(&$output, $item, $depth) {
$output .= '</li>'."\n";
}
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n".$indent.'<ul class="sub-menu dropdown">'."\n";
}
function end_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= $indent.'</ul>'."\n";
}
function display_element($element, &$children_elements, $max_depth, $depth=0, $args, &$output) {
$id_field = $this->db_fields['id'];
if (is_object($args[0])) {
$args[0]->has_children = ! empty($children_elements[$element->$id_field]);
}
return parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
} // end top bar walker
foundation-top-bar.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<div class="top-bar-container fixed contain-to-grid">
<nav class="top-bar">
<ul>
<li class="name">
<h1><a href="<?php echo home_url(); ?>"><?php bloginfo('name'); ?></a></h1>
</li>
<li class="toggle-topbar"><a href="#"></a></li>
</ul>
<section>
<?php foundation_top_bar_l(); ?>
 
<?php foundation_top_bar_r(); ?>
</section>
</nav>
</div>
readme.txt
1 2 3 4 5 6 7 8 9
foundation-menu.php and foundation-nav-walker.php should be included in the WordPress functions.php file.
 
The code in foundation-top-bar.php should be added below the body tag in header.php
 
Setup a menu in WordPress admin under Appearance > Menus
 
Use the section class on a menu item to create a menu section title
 
Foundation Top Bar Doc: http://foundation.zurb.com/docs/navigation.php

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.