Support

Home Forums Event Espresso Premium adding active class to menu-item when visiting cpt single event

adding active class to menu-item when visiting cpt single event

Posted: July 27, 2023 at 1:38 pm

Viewing 11 reply threads


Fysim_BV

July 27, 2023 at 1:38 pm

Hi,
I would like my menu item “Agenda” to be active and selected when visiting a single event. I’m working with a child theme and would love to get this code working. ‘post’ should be the id of the custom post type, but I prefer to change it the name of the post type itself. Any suggestions?

function add_custom_class($classes=array(), $menu_item=false) {
if ( is_single() && 'post' == get_post_type() && 'Agenda' == $menu_item->title && !in_array( 'current-menu-item', $classes ) ) {
$classes[] = 'current-menu-item'; // setting current menu item
}
return $classes;
}
add_filter('nav_menu_css_class', 'add_custom_class', 100, 2);

Thanks in advanced.


Tony

  • Support Staff

July 27, 2023 at 4:02 pm

Hi there,

My apologies, but I’m not really sure I follow what you are asking for.

If you are asking how to check for EE events then you can use:

if ( is_single() && 'espresso_events' == get_post_type() ) { }

Is that what you are looking for?


Fysim_BV

July 27, 2023 at 4:12 pm

Hi Tony,
I would like that the menu-item “Agenda” in my navigation is selected (active) when viewing single events from Event Espresso. Should I make a function in functions.php that would trigger that?


Tony

  • Support Staff

July 27, 2023 at 4:25 pm

Does the function you posted already do that, just not for EE events?


Fysim_BV

July 27, 2023 at 4:32 pm

function add_custom_class($classes=array(), $menu_item=false) {
    if ( is_single() && 'espresso_events' == get_post_type()  && 'Agenda' == $menu_item->title && !in_array( 'current-menu-item', $classes ) ) {
        $classes[] = 'current-menu-item'; // setting current menu item      
    }                         
    return $classes;
}
add_filter('nav_menu_css_class', 'add_custom_class', 100, 2); 

This code is not working. Any suggestions here?
Thanks in advanced!


Tony

  • Support Staff

July 27, 2023 at 4:41 pm

Does the function you posted work for non EE events?


Fysim_BV

July 27, 2023 at 11:21 pm

No, it doesn’t do anything at all.


Tony

  • Support Staff

July 31, 2023 at 4:45 am

Ok, so start from the beginning of the function and work out what isn’t working.

First, make sure the filter is actually running.

A quick an easy way is to just add something like:

print_r([$classes, $menu_item]);

As the first line of the hooked function and then make sure it when you view the menu on the front end it outputs all of the details you would expect. That also confirms that your filter is at least running.

Then swap location to be within your conditionals and refresh. If you are on an EE event it should output the same thing, outside of an EE event, nothing.

Is your theme actually calling a Menu that has been created within the dashboard?


Fysim_BV

July 31, 2023 at 5:28 am

This reply has been marked as private.


Tony

  • Support Staff

July 31, 2023 at 5:39 am

With no code changes at all, that code is doing exactly what you are requesting, it’s adding the current-menu-item class to the ‘Opleidingen’ menu item.

Is there something different you are expecting?

I suspect you need to also add the ‘active’ class:

$classes[] = 'current-menu-item'; // setting current menu item 
$classes[] = 'active'; // mark the menu item as active


Fysim_BV

July 31, 2023 at 5:45 am

Hi Tony,
This is the line of code I needed to let everything work as expected.
Thanks for helping me out.


Tony

  • Support Staff

July 31, 2023 at 5:47 am

Awesome, I’m glad that worked 🙂

Viewing 11 reply threads

The support post ‘adding active class to menu-item when visiting cpt single event’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso