Skip to content

Instantly share code, notes, and snippets.

@fieldoffice
Created March 12, 2020 10:11
Show Gist options
  • Save fieldoffice/7e4d9ca774e3c04264b9187bf65626ed to your computer and use it in GitHub Desktop.
Save fieldoffice/7e4d9ca774e3c04264b9187bf65626ed to your computer and use it in GitHub Desktop.
Custom Walker for WordPress. Removes default classes but keeps any custom classes
// Custom walker. Removes default classes but keeps any custom classes
class custom_walker extends Walker_Nav_Menu {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$class_names = join( ' ', apply_filters(
'nav_menu_css_class', (array)get_post_meta( $item->ID, '_menu_item_classes', true ) )
);
$output .= $indent . '';
$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 .= ! empty( $item->classes ) ? ' class="' . esc_attr( $class_names ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a'. $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 = 0, $args = array() ) {
$output .= "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment