Skip to content

Instantly share code, notes, and snippets.

@sadmansh
Created May 20, 2018 18:32
Show Gist options
  • Save sadmansh/b6855d3ed8a6ffee35c2626c4dfabffa to your computer and use it in GitHub Desktop.
Save sadmansh/b6855d3ed8a6ffee35c2626c4dfabffa to your computer and use it in GitHub Desktop.
WordPress navigation walker customized for Bulma
class Bulmaa_Nav_Walker extends Walker_Nav_Menu {
public function start_lvl(&$output, $depth = 0, $args = array()) {
$output .= '';
}
public function end_lvl(&$output, $depth = 0, $args = array()) {
$output .= '';
}
public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
if ($this->hasChildren($item)) {
$output .= $this->startDropdownButton($item);
} else {
$output .= $this->getLinkButton($item);
}
}
public function end_el(&$output, $item, $depth = 0, $args = array()) {
if ($this->hasChildren($item)) {
$output .= $this->endDropdownButton($item);
} else {
$output .= '';
}
}
public function hasChildren($item) {
if (in_array("menu-item-has-children", $item->classes)) {
return true;
}
return false;
}
public function getLinkButton($item) {
$url = $item->url ?? '';
$classes = empty($item->classes) ? array() : (array) $item->classes;
$class_names = '';
if (in_array('current-menu-item', $classes)) {
$class_names .= 'is-active';
}
$button = sprintf("<a href='%s' class='navbar-item %s'>%s</a>", $url, $class_names, $item->title);
return $button;
}
public function startDropdownButton($item) {
$url = $item->url ?? '';
$classes = empty($item->classes) ? array() : (array) $item->classes;
$class_names = '';
if (in_array('current-menu-item', $classes)) {
$class_names .= 'is-active';
}
$button = sprintf("<a href='%s' class='navbar-link %s'>%s</a>", $url, $class_names, $item->title);
$dropdown = sprintf("<div class='navbar-item has-dropdown is-hoverable'>%s", $button);
$dropdown .= "<div class='navbar-dropdown is-boxed'>";
return $dropdown;
}
public function endDropdownButton($item) {
return "</div></div>";
}
}
/*
Usage:
wp_nav_menu(
array(
'theme_location' => 'primary',
'menu' => '',
'walker' => new Bulma_Nav_Walker(),
'container' => false,
'items_wrap' => '<div class="navbar-end">%3$s</div>',
)
);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment