Skip to content

Instantly share code, notes, and snippets.

@roborourke
Last active December 10, 2015 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roborourke/4354916 to your computer and use it in GitHub Desktop.
Save roborourke/4354916 to your computer and use it in GitHub Desktop.
Super basic html5 menu output for WordPress
<?php
class Simple_Menu extends Walker_Nav_Menu {
// prevent sub menu wrapping
function start_lvl( &$output, $depth ) {
$output .= "\n";
}
function end_lvl( &$output, $depth ) {
$output .= "\n";
}
// menu items without <li> tags
function start_el( &$output, $item, $depth, $args ) {
global $wp_query;
$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent
// passed classes
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) );
// link 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 .= ' class="' . $class_names . '"';
$item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
$args->before,
$attributes,
$args->link_before,
apply_filters( 'the_title', $item->title, $item->ID ),
$args->link_after,
$args->after
);
// build html
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
function end_el( &$output, $item, $depth, $args ) {
$output .= "\n";
}
}
// Usage
wp_nav_menu( array(
'walker' => new Simple_Menu, // use the walker output from above
'container' => false, // remove container div
'items_wrap' => '<nav id="%1$s" class="%2$s">%3$s</nav>', // %3$s is replaced with the menu items
'depth' => 1 // prevent output of nested sub level links
) );
// Codex page:
// http://codex.wordpress.org/Function_Reference/wp_nav_menu
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment