- Sold between 250 000 and 1 000 000 dollars
- Won a Competition
- Author was Featured
- Item was Featured
- Exclusive Author
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Europe
- Bought between 10 and 49 items
Hi guys,
I hope you all having a great time!
How can I force Wordpress to highlight menu when particular content type is viewed?
Here’s an example.
I have a custom post type called “events” and menu structure:Home Events - Category name Contacts
When I’m clicking on the Events link I see all “events” custom posts and menu is correctly highlighted, but when I click on the event post itself menu isn’t highlighted (obviously because event post isn’t in the menu tree). The problem is that I don’t want to ask content editor to add each event post to the menu tree.
Is there a workaround for that?
Thanks and I’m really hoping to get some clues from the priceless ThemeForest members :))
- Author had a Free File of the Month
- Author was Featured
- Bought between 10 and 49 items
- Egypt
- Exclusive Author
- Has been a member for 2-3 years
- Item was Featured
- Referred between 10 and 49 users
- Sold between 10 000 and 50 000 dollars
Try writing your own menu Walker, I think it will do the trick. Kriesi has a tutorial for writing custom walker class to add an item description to the menu, you could use the same concept to add a current-menu-item class to menu on a certain content type using if(get_post_type() == 'event')
http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output
- Sold between 250 000 and 1 000 000 dollars
- Won a Competition
- Author was Featured
- Item was Featured
- Exclusive Author
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Europe
- Bought between 10 and 49 items
wizylabs said
Try writing your own menu Walker
Yes, I was thinking about same approach. But problem that I see now is that, when I’ll be going throw the menu with custom walker, I can’t classify links in the menu. They looks absolutely the same, except title and href of course 
- Author had a Free File of the Month
- Author was Featured
- Bought between 10 and 49 items
- Egypt
- Exclusive Author
- Has been a member for 2-3 years
- Item was Featured
- Referred between 10 and 49 users
- Sold between 10 000 and 50 000 dollars
Well you want to add the current class into the li not the anchor, right?
- Author had a Free File of the Month
- Author was Featured
- Bought between 10 and 49 items
- Egypt
- Exclusive Author
- Has been a member for 2-3 years
- Item was Featured
- Referred between 10 and 49 users
- Sold between 10 000 and 50 000 dollars
You wanna try this http://dl.dropbox.com/u/10477660/custom%20walker.php
I have wrote the walker for you, try it and see if it works, I haven’t tested it though
- Sold between 250 000 and 1 000 000 dollars
- Won a Competition
- Author was Featured
- Item was Featured
- Exclusive Author
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Europe
- Bought between 10 and 49 items
Thanks wizylabs for your help, your code is adding class name to all menu items when viewing “event” post types.
I came up with solution, I’m adding custom class name to the top menu items with help of WP menu manager, and based on that class I can classify menu links in the custom walker and then with simple if statement assign current-ancestor class 
No need for a walker, add a filter to ‘nav_menu_css_class’, do some if statements or switch to check which post type is it and according to that add a class to the parent menu item.
- Sold between 250 000 and 1 000 000 dollars
- Won a Competition
- Author was Featured
- Item was Featured
- Exclusive Author
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Europe
- Bought between 10 and 49 items
wpCanyonThemes said
No need for a walker, add a filter to ‘nav_menu_css_class’, do some if statements or switch to check which post type is it and according to that add a class to the parent menu item.
Sorry didn’t get your approach, could you spread some more light please?
Wrote a post on my blog last week on how to add additional classes to active nav menu page item.
add_filter( 'nav_menu_css_class', 'additional_active_item_classes', 10, 2 );
function additional_active_item_classes($classes = array(), $menu_item = false){
if(in_array('current-menu-item', $menu_item->classes)){
$classes[] = 'active';
}
return $classes;
}
Mess around with it a bit, do print_r() on $menu_item to see what’s returned in that array and change the code a bit to do what you need.
- Sold between 250 000 and 1 000 000 dollars
- Won a Competition
- Author was Featured
- Item was Featured
- Exclusive Author
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Europe
- Bought between 10 and 49 items
wpCanyonThemes said
Wrote a post on my blog last week on how to add additional classes to active nav menu page item.
Thanks very interesting post, but it’s doing different thing. Anyway thanks for sharing that tip – bookmarked 
