The markup of the wp_nav_menu
can be totally customized by using the Walker
class.
Here’s the code I used:
functions.php
class Walker_Menu extends Walker {
var $db_fields = array(
'parent' => 'menu_item_parent',
'id' => 'db_id'
);
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
if (is_front_page()) {
$output .= sprintf( "\n<li id='menu-item-%s'><a href="https://wordpress.stackexchange.com/questions/217716/%s">%s</a></li>\n",
sanitize_title($item->title),
$item->url,
$item->title
);
}
else {
$output .= sprintf( "\n<li id='menu-item-%s'%s><a href="https://wordpress.stackexchange.com/questions/217716/%s">%s</a></li>\n",
sanitize_title($item->title),
( $item->object_id == get_the_ID() ) ? ' class="current-menu-item"' : 'class="non-current-menu-item"',
$item->url,
$item->title
);
}
}
}
header.php
wp_nav_menu(array(
'menu' => 'Main Menu',
'walker' => new Walker_Menu()
));
It add the class non-current-menu-item to every li element but the current menu item, and it will not do so on the front page.
Huge thanks to dingo_d for pointing out the class!