Skip to content

Instantly share code, notes, and snippets.

@clausmith
Created May 17, 2020 01:36
Show Gist options
  • Save clausmith/10c8c9d3635edd891c0597e786e6af8b to your computer and use it in GitHub Desktop.
Save clausmith/10c8c9d3635edd891c0597e786e6af8b to your computer and use it in GitHub Desktop.
A custom nav walker for WordPress that uses the Bulma navbar styles (from bikubi/wp-bulma-navbar-walker)
<?php
/**
* Custom nav menu walker with Bulma support
*
* @link https://developer.wordpress.org/reference/classes/walker_nav_menu/
*
* @package Bureau
*/
class Bulma_Nav_Menu extends Walker_Nav_Menu {
public function start_lvl( &$output, $depth = 0, $args = array() ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = str_repeat( $t, $depth );
$classes = array( 'navbar-dropdown', 'sub-menu' );
$class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$output .= "{$n}{$indent}<div$class_names>{$n}";
}
public function end_lvl( &$output, $depth = 0, $args = array() ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = str_repeat( $t, $depth );
$output .= "$indent</div>{$n}";
}
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'navbar-item';
$classes[] = 'menu-item-' . $item->ID;
if ($args->walker->has_children) {
$classes[] = 'has-dropdown';
$classes[] = 'is-hoverable';
}
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
if ( $args->walker->has_children ) {
$output .= $indent . '<div' . $id . $class_names .'>';
}
else {
$output .= $indent;
}
$atts = array();
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
if ($args->walker->has_children) {
$atts['class'] = 'navbar-link';
}
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
$title = apply_filters( 'the_title', $item->title, $item->ID );
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
$item_output = $args->before;
if ($args->walker->has_children) {
$item_output .= '<a'. $attributes .'>';
}
else {
$item_output .= '<a'. $id . $class_names . $attributes .'>';
}
$item_output .= $args->link_before . $title . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
if ( $args->walker->has_children ) {
$output .= "</div>{$n}";
}
else {
$output .= "{$n}";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment