Skip to content

Instantly share code, notes, and snippets.

@pontusab
Last active December 14, 2015 09:18
Show Gist options
  • Save pontusab/5063555 to your computer and use it in GitHub Desktop.
Save pontusab/5063555 to your computer and use it in GitHub Desktop.
WordPress simple bootstrap-menu like this: http://twitter.github.com/bootstrap/components.html#navbar
<?php wp_nav_menu( array(
'theme_location' => 'primary',
'container' => false,
'items_wrap' => '<ul class="nav">%3$s</ul>',
'walker' => new Bootstrap()
) );
?>
<?php
/**
* Class Name: Bootstrap
* Author: Pontus Abrahamsson
*/
class Bootstrap extends Walker_Nav_Menu
{
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
{
$sel = null;
if( is_array( $item->classes ) && in_array( 'current-page-ancestor', $item->classes ) || $item->current )
{
$sel = 'active';
}
$output .= '<li '. ( isset( $sel ) ? 'class="'. $sel .'"' : null ) .'>';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->title .' '. $item->attr_title ) .'"' : null;
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : null;
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : null;
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : null;
$item_output = '<a'. $attributes .'>';
$item_output .= apply_filters( 'the_title', $item->title, $item->ID );
$item_output .= '</a>';
$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