Skip to content

Instantly share code, notes, and snippets.

@jeremyfelt
Created August 22, 2012 23:50
Show Gist options
  • Save jeremyfelt/3430653 to your computer and use it in GitHub Desktop.
Save jeremyfelt/3430653 to your computer and use it in GitHub Desktop.
Add a default 'current-menu-item' class
add_filter( 'wp_nav_menu_objects', 'edit_nav_main_objects', 10, 2 );
/**
* Parse nav menu items and add a current-menu-item class to the first menu item
* in in the menu if no current-menu-item currently exists.
*
* @param $current_menu_items array of objects containing menu items
* @param $current_menu object containing menu information
*
* @return mixed array of objects containing menu items
*/
function edit_nav_main_objects( $current_menu_items, $current_menu ) {
if ( ! isset( $current_menu->theme_location ) || 'header' != $current_menu->theme_location )
return $current_menu_items;
$current_classes = wp_list_pluck( $current_menu_items, 'classes' );
foreach ( $current_classes as $c ) {
if ( in_array( 'current-menu-item', $c ) )
return $current_menu_items;
}
$current_menu_items[1]->classes[] = 'current-menu-item';
return $current_menu_items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment