Skip to content

Instantly share code, notes, and snippets.

@TMMC
Created July 7, 2017 08:32
Show Gist options
  • Save TMMC/81012f34048187ffa91fe331bf9db55c to your computer and use it in GitHub Desktop.
Save TMMC/81012f34048187ffa91fe331bf9db55c to your computer and use it in GitHub Desktop.
wp nav walker
<?php
/**
* Top navigation and branding
*/
?>
<!-- <div class="utilsbar">
<div class="container">
some info here
</div>
</div> -->
<header id="masthead" class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle collapsed" type="button" title="Rozwiń menu" data-toggle="collapse" data-target="#nav-main" aria-expanded="false" aria-controls="nav-main">
<span class="sr-only">Menu</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<h1>
<a href="<?php echo esc_url( home_url( '/' ) ); ?>" class="navbar-brand" title="<?php bloginfo( 'name' ); ?>">
<?php
if ( has_custom_logo() ) {
$custom_logo_id = get_theme_mod( 'custom_logo' );
$custom_logo = wp_get_attachment_image_src( $custom_logo_id , 'full' );
echo '<img src="'. esc_url( $custom_logo[0] ) .'" alt="'. get_bloginfo( 'name' ) .'" />';
} else {
bloginfo( 'name' );
}
?>
</a>
</h1>
</div>
<?php wp_nav_menu( array(
'menu' => 'primary',
'theme_location' => 'primary',
'depth' => 2,
'menu_id' => 'nav-main-list',
'menu_class' => 'nav navbar-nav',
'container' => 'nav',
'container_class' => 'navbar-collapse navbar-right collapse',
'container_id' => 'nav-main',
'walker' => new Startertheme_Primary_Nav_Walker()
// 'fallback_cb' => 'WP_Bootstrap_Navwalker::fallback',
// 'walker' => new WP_Bootstrap_Navwalker()
) ); ?>
</div>
</header>

Classes used for various options:

  1. .ni_<icon-prefix> - glyphicon/fa (first icon class)
  2. .ni_<icon-name> - glyphicon-whatever (second icon class)
  3. ni_hidetext - generate <span class="sr-only">Something</span>
  4. ni_disabled
<?php
/**
* WP Custom Nav Walker
*/
if (!class_exists('Startertheme_Primary_Nav_Walker')) {
class Startertheme_Primary_Nav_Walker extends Walker_Nav_Menu {
function start_lvl(&$output, $depth = 0, $args = array()) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul role=\"menu\" class=\"dropdown-menu\">\n";
}
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
$indent = ($depth) ? str_repeat("\t", $depth) : '';
$li_attributes = '';
$value = '';
$class_names = $value;
$classes = empty($item->classes) ? array() : (array) $item->classes;
if (in_array("ni_hidetext", $classes)) {
$srOpen = '<span class="sr-only">';
$srClose = '</span>';
} else {
$srOpen = $srClose = '';
}
if (in_array("ni_disabled", $classes)) {
$classes[] = 'disabled';
}
$navItemOptions = array_filter($classes, function($var) {
$pat = '/^ni_/i';
return preg_match($pat, $var);
});
$navItemIcon = '';
if (!empty($navItemOptions)) {
$classes = array_diff($classes, $navItemOptions); // remove unnecessary classes from `li`
$navItemIconClasses = array_filter($navItemOptions, function($var) {
$pat = '/^ni_(?!hidetext|disabled)/i';
return preg_match($pat, $var);
});
if (!empty($navItemIconClasses)) {
$navItemIcon = '<i class="' . str_replace('ni_', '', join(' ', $navItemIconClasses)) . '" aria-hidden="true"></i> ';
}
}
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
if ($args->walker->has_children) {$class_names .= ' dropdown';}
if ($item->current || $item->current_item_ancestor) {$class_names .= ' active';}
$class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
$id = apply_filters('nav_menu_item_id', 'menu-item-'.$item->ID, $item, $args);
$id = strlen($id) ? ' id="' . esc_attr($id) . '"' : '';
$output .= $indent . '<li' . $id . $value . $class_names . $li_attributes . '>';
$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) . '"' : '';
$attributes .= ($args->walker->has_children) ? ' class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true"' : '';
$item_output = $args->before;
$item_output .= '<a' . $attributes . '>' . $navItemIcon . $srOpen;
$item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
$item_output .= ($depth === 0 && $args->walker->has_children) ? $srClose . ' <i class="glyphicon glyphicon-menu-down" aria-hidden="true"></i></a>' : $srClose . '</a>';
$item_output .= $args->after;
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
// function end_el() {
//
// }
// function end_lvl() {
//
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment