Skip to content

Instantly share code, notes, and snippets.

@brentini
Last active March 23, 2018 21:32
Show Gist options
  • Save brentini/3158535 to your computer and use it in GitHub Desktop.
Save brentini/3158535 to your computer and use it in GitHub Desktop.
Walker class for WordPress custom menu integration in Twitter Bootstrap navbar #wordpress
<?php
class Walker_Navbar_Menu extends Walker_Nav_Menu {
public $dropdown_enqueued;
function __construct() {
$this->dropdown_enqueued = wp_script_is( 'bootstrap-dropdown', 'queue' );
}
function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) {
if ( $element->current )
$element->classes[] = 'active';
$element->is_dropdown = ( 0 == $depth ) && ! empty( $children_elements[$element->ID] );
if ( $element->is_dropdown )
$element->classes[] = 'dropdown';
parent::display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output );
}
function start_lvl( &$output, $depth ) {
if ( ! $this->dropdown_enqueued ) {
wp_enqueue_script( 'bootstrap-dropdown' );
$this->dropdown_enqueued = true;
}
$indent = str_repeat( "\t", $depth );
$class = ( 0 == $depth ) ? 'dropdown-menu' : 'unstyled';
$output .= "\n{$indent}<ul class='{$class}'>\n";
}
function start_el( &$output, $item, $depth, $args ) {
$item_html = '';
parent::start_el( &$item_html, $item, $depth, $args );
if ( $item->is_dropdown && ( 1 != $args->depth ) ) {
$item_html = str_replace( '<a', '<a class="dropdown-toggle" data-toggle="dropdown"', $item_html );
$item_html = str_replace( '</a>', '<b class="caret"></b></a>', $item_html );
$item_html = str_replace( esc_attr( $item->url ), '#', $item_html );
}
$output .= $item_html;
}
}
<div id="navbar-menu" class="navbar">
<div class="navbar-inner">
<?php
wp_nav_menu( array(
'theme_location' => 'navbar',
'container_class' => 'container',
'menu_class' => 'nav',
'fallback_cb' => '',
'walker' => new Walker_Navbar_Menu(),
) );
?>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment