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

  • 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 Oct 10 '12 at 8:16
  • @userabuser yes, that is correct – Zohar Oct 10 '12 at 15:55

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.

| 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
  • This doesn't work for me, no classes are being added to the stated menu item. – Lee Aug 31 '18 at 19:01

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 );
| improve this answer | |
  • This always applies the class, regardless of if the menu item is currently viewed or not. – Lee Aug 31 '18 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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