Add the current menu item CSS class to a custom page type archive in WordPress menu

I’ve been reading a lot about this but couldn’t find a solution.

Basically I’m trying to show a current menu item on a dynamic page that is created by a custom page type archive.
The default menu control doesn’t add the current-menu-item CSS class names.
I’ve found a solution here – http://bloggingsquared.com/blogging-tips/how-to-add-the-current-menu-item-css-class-to-a-custom-link-in-a-wordpress-menu/

Which is like this:

add_filter('nav_menu_css_class', 'AddCurrentMenuItemClass',1,2);

function AddCurrentMenuItemClass($classes,$item)
{
    $link = site_url().$_SERVER['REQUEST_URI'];
    if(strpos($link, $item->url) !== false)
    {
        $classes[] = 'current-menu-item';
    }
    return $classes;
}

The problem I’m having is that the Home page (which is a custom menu item) is highlighted as well as the page I’m on.
If I change the if statement to:

     if ( $link == $item->url )

Then in works on that page as but not on sub pages.

Can you please help me sort this out?
I’m working on a client project with a right deadline…

I’m on WP 3.4.2 with a premium WooThemes template: Whitelight

2 Answers
2

This is what you want to do,

  add_filter( 'wp_get_nav_menu_items', 'cpt_archive_menu_filter', 10, 3 );
  function cpt_archive_menu_filter( $items, $menu, $args ) {
    foreach ( $items as &$item ) {
    if ( $item->type != 'custom' ) continue;
      if (  get_query_var( 'post_type' ) == 'your-post-type' && $item->title == 'Title of Link' ) {
        $item->classes []= 'class-name-your-want';
        } 
    }
    return $items;
  } 

Although the above will work, Custom Links get assigned the current-menu-item class so this shouldn’t be necessary, if I follow you correctly.

Leave a Comment