Skip to content

Instantly share code, notes, and snippets.

@cassler
Created February 7, 2014 04:15
Show Gist options
  • Save cassler/8857307 to your computer and use it in GitHub Desktop.
Save cassler/8857307 to your computer and use it in GitHub Desktop.
Bootstrap Wordpress Nav Walker, changes markup on your menus to use bootstrap formatting
<?php class responsive_menu_walker extends Walker_Nav_Menu
{
function start_lvl(&$output, $depth) {
// update the <li> element
$bookmark = 'den-menu-top';
$li_pos = strrpos ( $output, $bookmark );
$output = substr($output, 0, $li_pos+strlen($bookmark)) . " dropdown" . substr($output, $li_pos+strlen($bookmark), strlen($output) - $li_pos);
// update the <a> element
$bookmark = "<a ";
$anchor_pos = strrpos ( $output, $bookmark );
$output = substr($output, 0, $anchor_pos+strlen($bookmark)) . substr($output, $anchor_pos+strlen($bookmark), strlen($output) - $anchor_pos);
$output .= "<span class=\"disclosure-down\"></span><ul class=\"dropdown-menu sub-menu sub-menu-". $depth ."\">";
}
function end_lvl(&$output, $depth) {
$output .= "</ul>";
}
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;
$page_info = get_page($item->object_id); // get the page slug
$page_slug = $page_info->post_name;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
if ( $item->menu_item_parent == 0 ? $parent_item = ' den-menu-top' : $parent_item = '' );
$class_names = ' class="'. esc_attr( $class_names ) . ' nb-' . $page_slug . $parent_item . '"';
$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 ) .'"' : '';
$prepend = '';
$append = '';
$description = ! empty( $item->description ) ? ' <span>'.esc_attr( $item->description ).'</span>' : '';
if($depth != 0)
{
$description = $append = $prepend = "";
}
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
$item_output .= $description.$args->link_after;
$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