View difference between Paste ID: U2ZNM4eQ and u510cJ8D
SHOW: | | - or go back to the newest paste.
1
// Modified from http://www.rarescosma.com/2010/11/add-a-class-to-wp_nav_menu-items-with-urls-included-in-the-current-url/
2
// Need to add a class (using 'current-menu-item' to the nav menu to highlight it 
3
// when on custom post type pages (e.g. "/movies/" ) 
4
// Calendar needs to look for "Calendar, Venues, 
5
// (and in this case Organizers, if you've updated the post_type with a slug)
6
function add_parent_url_menu_class( $classes = array(), $item = false ) {
7
	// Get current URL
8
	$current_url = current_url();
9
	
10
	// Get homepage URL
11
	$homepage_url = trailingslashit( get_bloginfo( 'url' ) );
12
	
13
	// Exclude 404 and homepage
14
	if( is_404() or $item->url == $homepage_url ) return $classes;
15
	
16
	// if $item->url can be found in $current_url
17
	// OR if $current_url (haystack) contains organizer, venue, events
18
	// AND $item->url is blah/calendar, add class
19
	if ( strstr( $current_url, $item->url) || ( ( strstr( $current_url, '/events/' ) || strstr ($current_url, '/organizer/') || strstr($current_url,'/venue/') )  && strstr( $item->url, '/calendar/') ) ) {
20
		// Add the 'current-menu-item' class
21
		$classes[] = 'current-menu-item';
22
	}
23
24
	return $classes;
25
}
26
add_filter( 'nav_menu_css_class', 'add_parent_url_menu_class', 10, 2 );
27
28
// this is a helper function to the above add_parent_url_menu_class;
29
// it checks the current url and pulls all the universal stuff out
30
function current_url() {
31
	// Protocol
32
	$url = ( 'on' == $_SERVER['HTTPS'] ) ? 'https://' : 'http://';
33
34
	$url .= $_SERVER['SERVER_NAME'];
35
36
	// Port
37
	$url .= ( '80' == $_SERVER['SERVER_PORT'] ) ? '' : ':' . $_SERVER['SERVER_PORT'];
38
39
	$url .= $_SERVER['REQUEST_URI'];
40
41
	return trailingslashit( $url );
42
}