819 posts
  • Power Elite Author: Sold between 1 000 000 - 1 999 999 dollars
  • Power Elite Author
  • Won a Competition
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Bought between 10 and 49 items
+2 more
mixey says

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 :))

1012 posts
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Egypt
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 10 and 49 users
  • Sold between 10 000 and 50 000 dollars
+1 more
wizylabs says

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

819 posts
  • Power Elite Author: Sold between 1 000 000 - 1 999 999 dollars
  • Power Elite Author
  • Won a Competition
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Bought between 10 and 49 items
+2 more
mixey says

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 :)

1012 posts
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Egypt
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 10 and 49 users
  • Sold between 10 000 and 50 000 dollars
+1 more
wizylabs says

Well you want to add the current class into the li not the anchor, right?

1012 posts
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Egypt
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 10 and 49 users
  • Sold between 10 000 and 50 000 dollars
+1 more
wizylabs says

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

819 posts
  • Power Elite Author: Sold between 1 000 000 - 1 999 999 dollars
  • Power Elite Author
  • Won a Competition
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Bought between 10 and 49 items
+2 more
mixey says

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 :)

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

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.

819 posts
  • Power Elite Author: Sold between 1 000 000 - 1 999 999 dollars
  • Power Elite Author
  • Won a Competition
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Bought between 10 and 49 items
+2 more
mixey says

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?

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

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.

819 posts
  • Power Elite Author: Sold between 1 000 000 - 1 999 999 dollars
  • Power Elite Author
  • Won a Competition
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Bought between 10 and 49 items
+2 more
mixey says

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 ;)

by
by
by
by
by
by