Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress nav menu custom current URL ancestor
<?php
/**
* @see https://github.com/WordPress/WordPress/blob/3.9.1/wp-includes/nav-menu-template.php#L351-L358
*/
function prefix_wp_nav_menu_objects( $sorted_menu_items, $args ) {
// @see http://wordpress.stackexchange.com/a/126534
$current_url = home_url( add_query_arg( null, null ) );
foreach ( $sorted_menu_items as $menu_item ) {
if ( strpos( $current_url, $menu_item->url ) === 0 ) {
$menu_item->classes[] = 'prefix-current-url-ancestor';
}
}
return $sorted_menu_items;
}
add_filter( 'wp_nav_menu_objects', 'prefix_wp_nav_menu_objects', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.