Created
July 30, 2014 12:27
-
-
Save remcotolsma/17f50704621f0de4e9bc to your computer and use it in GitHub Desktop.
WordPress nav menu custom current URL ancestor
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
<?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