I'm currently using a WordPress custom Walker to customize my navigation. It currently generates this navigation:
<nav class="nav-top clearfix">
<ul class="clearfix">
<li id="menu-item-69" class="current_page_item"><a href="#">test1</a></li>
<li id="menu-item-68"><a href="#">test2</a></li>
<li id="menu-item-67"><a href="#">test3</a></li>
<li id="menu-item-66"><a href="#">test4</a></li>
<li id="menu-item-65"><a href="#">test5</a></li>
<li id="menu-item-64"><a href="#">test6</a></li>
</ul>
</nav>
When i click on test2, it will apply the current_page_item class to it.
Everything is working fine, however I also want to use the same navigation in the footer. If I do that, then i get validation errors because of the li id. How do i change the li id to li class ?
Here's my custom Walker:
class My_Walker extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth, $args)
{
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$current_indicators = array('current_page_item');
$newClasses = array();
foreach($classes as $el){
//check if it's indicating the current page, otherwise we don't need the class
if (in_array($el, $current_indicators)){
array_push($newClasses, $el);
}
}
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $newClasses), $item ) );
if($class_names!='') $class_names = ' class="'. esc_attr( $class_names ) . '"';
$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
$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 ) .'"' : '';
if($depth != 0)
{
//children stuff, maybe you'd like to store the submenu's somewhere?
}
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before .apply_filters( 'the_title', $item->title, $item->ID );
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
In case you haven't figured that out yet, here's what you should do:
Replace $newClasses = array();
with:
$newClasses = array( 'menu-item-'. $item->ID );
Then replace the following line:
$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
With:
$output .= $indent . '<li' . $value . $class_names .'>';