2

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
  • 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?
    – Adam
    CommentedOct 10, 2012 at 8:16
  • @userabuser yes, that is correct
    – Zohar
    CommentedOct 10, 2012 at 15:55

3 Answers 3

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.

2
  • 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
    CommentedOct 10, 2012 at 16:11
  • This doesn't work for me, no classes are being added to the stated menu item.
    – Lee
    CommentedAug 31, 2018 at 19:01
0

This works out-of-the-box, thanks for the heavy part of the work to both of you.

add_filter( 'wp_get_nav_menu_items', function($items, $menu, $args) { foreach ($items as &$item) { if (is_single()) { if (strpos(site_url().$_SERVER['REQUEST_URI'], $item->url) !== false) { $item->classes[] = 'current-menu-item'; } } } return $items; }, 10, 3); 
    -2

    I had the same problem. This is what I do: Change the YourMenuItem to your menu name item.

     /* Add a custom class to a specific menu item */ function my_class( $classes, $item ) { if ( $item->title == "YourMenuItem" ) { $classes[] = 'current-page'; } return $classes; } add_filter( 'nav_menu_css_class', 'my_class', 10, 2 ); 
    1
    • This always applies the class, regardless of if the menu item is currently viewed or not.
      – Lee
      CommentedAug 31, 2018 at 19:00

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.