1383 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 10+ members
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
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
  • Has been part of the Envato Community for over 5 years
  • Has referred 10+ members
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
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
  • Has been part of the Envato Community for over 6 years
  • Has referred 1+ members
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
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
  • Has been part of the Envato Community for over 6 years
  • Has referred 1+ members
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
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

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by