Created
August 22, 2012 23:50
-
-
Save jeremyfelt/3430653 to your computer and use it in GitHub Desktop.
Add a default 'current-menu-item' class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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