Skip to content

Instantly share code, notes, and snippets.

@mikaeljorhult
Created June 4, 2012 21:31
Show Gist options
  • Save mikaeljorhult/2870948 to your computer and use it in GitHub Desktop.
Save mikaeljorhult/2870948 to your computer and use it in GitHub Desktop.
Walker_Menu_Class outputting only plain anchor links.
<?php
class Stripped_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
global $wp_query, $post;
$indent = ($depth) ? str_repeat("\t", $depth) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
if(is_object($post)) {
$post_type = get_post_type($post->ID);
$active_post_type = get_post_meta($item->object_id, 'menu_post_type', true);
if($post_type == $active_post_type || ($post_type == 'post' && rtrim($item->url, '/') == rtrim(get_bloginfo('url'), '/'))) {
$classes[] = 'current_page_item';
}
}
$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 ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a' . $id . $value . $class_names . $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
function end_el(&$output, $item, $depth) {}
function start_lvl(&$output, $depth) {}
function end_lvl(&$output, $depth) {}
}
function stripped_remove_list($nav_menu) {
return preg_replace('/<\/?ul(.?||.+?)>/i', '', $nav_menu);
}
add_filter('wp_nav_menu', 'stripped_remove_list');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment