Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Need to add a class (using 'current-menu-item' to the nav menu to highlight it
- // when on custom post type pages (e.g. "/movies/" )
- // Calendar needs to look for "Calendar, Venues,
- // (and in this case Organizers, if you've updated the post_type with a slug)
- function add_parent_url_menu_class( $classes = array(), $item = false ) {
- // Get current URL
- $current_url = current_url();
- // Get homepage URL
- $homepage_url = trailingslashit( get_bloginfo( 'url' ) );
- // Exclude 404 and homepage
- if( is_404() or $item->url == $homepage_url ) return $classes;
- // if $item->url can be found in $current_url
- // OR if $current_url (haystack) contains organizer, venue, events
- // AND $item->url is blah/calendar, add class
- if ( strstr( $current_url, $item->url) || ( ( strstr( $current_url, '/events/' ) || strstr ($current_url, '/organizer/') || strstr($current_url,'/venue/') ) && strstr( $item->url, '/calendar/') ) ) {
- // Add the 'current-menu-item' class
- $classes[] = 'current-menu-item';
- }
- return $classes;
- }
- add_filter( 'nav_menu_css_class', 'add_parent_url_menu_class', 10, 2 );
- // this is a helper function to the above add_parent_url_menu_class;
- // it checks the current url and pulls all the universal stuff out
- function current_url() {
- // Protocol
- $url = ( 'on' == $_SERVER['HTTPS'] ) ? 'https://' : 'http://';
- $url .= $_SERVER['SERVER_NAME'];
- // Port
- $url .= ( '80' == $_SERVER['SERVER_PORT'] ) ? '' : ':' . $_SERVER['SERVER_PORT'];
- $url .= $_SERVER['REQUEST_URI'];
- return trailingslashit( $url );
- }
Advertisement
Add Comment
Please, Sign In to add comment