Classes used for various options:
.ni_<icon-prefix>
- glyphicon/fa (first icon class).ni_<icon-name>
- glyphicon-whatever (second icon class)ni_hidetext
- generate<span class="sr-only">Something</span>
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() { | |
// | |
// } | |
} | |
} |