• Resolved paulw80

    (@paulw80)


    I have created a custom post type called ‘teams’, this is working ok but when I click on teams on the menu the current menu item is not highlighting, I found this snippet to add to the funtions.php file which successfully removed the highlighting of the normal wordpress posts page which is the ‘training page’, im stumped with this one!, does anyone have any idea how to get this working?

    Cheers!

    add_editor_style( 'editor-style.css' );
    
    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 ( get_post_type() == "team" )
      {
        unset($classes[array_search('current_page_parent',$classes)]);
        if ( isset($item->url) )
          if ( strstr( $current_url, $item->url) )
            $classes[] = 'current-menu-item';
      }
    
      return $classes;
    }

    [please mark any posted code – http://codex.wordpress.org/Forum_Welcome#Posting_Code ]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter paulw80

    (@paulw80)

    Sorry it would be helpful if I gave the site url!

    http://www.fior.org.uk/team/

    doh…

    Thread Starter paulw80

    (@paulw80)

    ok, to anyone else that may be having this annoying issue….adding the code below to my functions.php file solved the problem.

    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 ( strstr( $current_url, $item->url) ) {
    		// Add the 'parent_url' class
    		$classes[] = 'current-menu-item current_page_item';
    	}
    
    	return $classes;
    }
    
    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 );
    }
    
    add_filter( 'nav_menu_css_class', 'namespace_menu_classes', 10, 2 );
    function namespace_menu_classes( $classes , $item ){
    	if ( get_post_type() == 'event' || is_archive( 'event' ) ) {
    		// remove that unwanted classes if it's found
    		$classes = str_replace( 'current_page_parent', '', $classes );
    		// find the url you want and add the class you want
    		if ( $item->url == '/events' ) {
    			$classes = str_replace( 'menu-item', 'menu-item current_page_item', $classes );
    			remove_filter( 'nav_menu_css_class', 'namespace_menu_classes', 10, 2 );
    		}
    	}
    	return $classes;
    }
    rdueck

    (@rdueck)

    Thanks for the share. This is exactly what I’m looking for, but as I’m not a programmer, Can you tell me which items to change so that it can work for my website?

    Thanks.

    The above appears a little overcomplicated; a slimmer version would be:

    // highlight active custom post page in nav
    add_filter( 'nav_menu_css_class', 'namespace_menu_classes', 10, 2 );
    function namespace_menu_classes( $classes , $item ){
    	if ( get_post_type() == 'YOUR_CUSTOM_POST_TYPE' ) {
    		// remove unwanted classes if found
    		$classes = str_replace( 'current_page_parent', '', $classes );
    		// find the url you want and add the class you want
    		if ( $item->url == '/PATH_TO_YOUR_ARCHIVE' ) {
    			$classes = str_replace( 'menu-item', 'menu-item current_page_parent', $classes );
    		}
    	}
    	return $classes;
    }

    Once you change YOUR_CUSTOM_POST_TYPE to the machine name of your custom post name, this should add the class ‘current_page_parent’ to your active custom post menu item, and remove the same class from any other menu items.

    Also, change ‘/PATH_TO_YOUR_ARCHIVE’ to the actual path of your archive.

    For example, in the below I have a custom post type called ‘Children’s Books’, and set up the post type like so (in functions.php):

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
    	register_post_type( 'childrens_books',
    		array(
    			'labels' => array(
    				'name' => __( 'Children's Books' ),
    				'singular_name' => __( 'Children's Book' )
    			),
    		'public' => true,
    		'has_archive' => true,
    		'rewrite' => array('slug' => 'childrens-books'),
    		)
    	);
    }

    In order for the menu items to highlight properly I would use the following (again, in functions.php):

    // highlight active custom post page in nav
    add_filter( 'nav_menu_css_class', 'namespace_menu_classes', 10, 2 );
    function namespace_menu_classes( $classes , $item ){
    	if ( get_post_type() == 'childrens_books' ) {
    		// remove unwanted classes if found
    		$classes = str_replace( 'current_page_parent', '', $classes );
    		// find the url you want and add the class you want
    		if ( $item->url == '/childrens-books' ) {
    			$classes = str_replace( 'menu-item', 'menu-item current_page_parent', $classes );
    		}
    	}
    	return $classes;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom post type – highlighting current menu item’ is closed to new replies.