Skip to content

Instantly share code, notes, and snippets.

@joshbetz
Last active December 12, 2015 01:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshbetz/4688292 to your computer and use it in GitHub Desktop.
Save joshbetz/4688292 to your computer and use it in GitHub Desktop.
WordPress nav menu without lists.
<?php
class v12_Walker_Nav_Menu extends Walker_Nav_Menu {
function __construct() {
add_filter( 'wp_nav_menu_args', array( __CLASS__, 'items_wrap' ) );
}
/**
* @see wp_nav_menu()
*
* @param array $args Arguments for wp_nav_menu
*/
static function items_wrap( $args ) {
$args['container'] = '';
$args['items_wrap'] = '<nav id="%1$s" class="%2$s">%3$s</nav>';
$args['fallback_cb'] = '__return_false';
return $args;
}
/**
* @see Walker::start_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<div class=\"sub-menu\">\n";
}
/**
* @see Walker::end_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
function end_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</div>\n";
}
/**
* @see Walker::start_el()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param int $current_page Menu item ID.
* @param object $args
*/
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
$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 .= $id . $value . $class_names;
$item_output = ! empty( $args->before ) ? $args->before : '';
$item_output .= '<a'. $attributes .'>';
$item_output .= ! empty( $args->links_before ) ? $args->links_before : '';
$item_output .= apply_filters( 'the_title', $item->title, $item->ID );
$item_output .= ! empty( $args->links_after ) ? $args->links_after : '';
$item_output .= '</a>';
$item_output .= ! empty( $args->after ) ? $args->after : '';
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
/**
* @see Walker::end_el()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Page data object. Not used.
* @param int $depth Depth of page. Not Used.
*/
function end_el( &$output, $item, $depth = 0, $args = array() ) {
$output .= "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment