1383 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
  • Serbia
wpCanyonThemes says

Tomorrow i’ll probably need to do the same thing as you do now, and will use a filter instead of walker, will post the code on the blog :)

1383 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
  • Serbia
wpCanyonThemes says

Here you go a quick solution, i’ll write a better one which will be dynamic so it works with all post types except page (blog post and custom post types) and will require no changes, will work automatically. Will post it on the blog when it’s done :)

add_filter('nav_menu_css_class', 'current_page_for_events', 10, 2);
function current_page_for_events($classes = array(), $menu_item = false){

    $c_page_type = get_post_type();
    $c_nav_item_tpl = get_post_meta($menu_item->object_id, '_wp_page_template', true);

    if($c_page_type == 'events' && $c_nav_item_tpl == 'template-events.php'){
        $classes[] = 'current-menu-item';
    }

    return $classes;
}

It will add the default current-menu-item class (the one WordPress adds), but you can change it to whatever you want or add new ones.

Also i’m assuming you have a custom template you use for the events listing which is added to the “Events” page, change the “template-events.php” to whatever the template file is called.

Tested and works :)

2 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 1 and 9 users
  • United States
fearlex says

Here you go a quick solution, i’ll write a better one which will be dynamic so it works with all post types except page (blog post and custom post types) and will require no changes, will work automatically. Will post it on the blog when it’s done :)
add_filter('nav_menu_css_class', 'current_page_for_events', 10, 2);
function current_page_for_events($classes = array(), $menu_item = false){

    $c_page_type = get_post_type();
    $c_nav_item_tpl = get_post_meta($menu_item->object_id, '_wp_page_template', true);

    if($c_page_type == 'events' && $c_nav_item_tpl == 'template-events.php'){
        $classes[] = 'current-menu-item';
    }

    return $classes;
}

It will add the default current-menu-item class (the one WordPress adds), but you can change it to whatever you want or add new ones.

Also i’m assuming you have a custom template you use for the events listing which is added to the “Events” page, change the “template-events.php” to whatever the template file is called.

Tested and works :)

Buddy, you saved my live. Wow, i’m spechless. Thank you so MUCH for this piece of code.

2 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 1 and 9 users
  • United States
fearlex says
Actually in my case i need something like this:

add_filter('nav_menu_css_class', 'current_page_for_category', 10, 2);
function current_page_for_category($classes = array(), $menu_item = false){

    if(is_category(array('cat-slug1','cat-slug2','cat-slug3','cat-slug4')))
    {
        print_r($classes);
        echo "<br />";

        //$classes[] = 'current-menu-item';
    }

    return $classes;
}

But after checking the output of print_r, i end up using the .current_page_parent class in my css for this.

At least for my specific case it worked. But i definitively learned something invaluable and new.

Thanks again

by
by
by
by
by
by