Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
The home page? You mean the link to the home page also has the current-menu-item class as well as the page you are viewing? Is that right? –  userabuser Oct 10 '12 at 8:16
    
@userabuser yes, that is correct –  Zohar Oct 10 '12 at 15:55

1 Answer 1

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.

share|improve this answer
    
Thanks for the answer, but I believe that the theme has a function to recognise the Home custom menu item. The home doesn't highlight when I'm on a regular page, only on the custom when the code I wrote above is on. –  Zohar Oct 10 '12 at 16:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.