Skip to content

Instantly share code, notes, and snippets.

@remcotolsma
Created July 30, 2014 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remcotolsma/17f50704621f0de4e9bc to your computer and use it in GitHub Desktop.
Save remcotolsma/17f50704621f0de4e9bc to your computer and use it in GitHub Desktop.
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