Posted By

ihatetomatoes on 06/01/13


Tagged

wordpress


Versions (?)

WP - Highlight nav item for custom post type archive and single


 / Published in: PHP
 

URL: http://wordpress.stackexchange.com/questions/3014/highlighting-wp-nav-menu-ancestor-class-w-o-children-in-nav-structure

For this to work add class "[CPT]-menu-item" to your menu item.

  1. <?php
  2. // The code below finds the menu item with the class "[CPT]-menu-item" and adds another “current_page_parent” class to it.
  3. // Furthermore, it removes the “current_page_parent” from the blog menu item, if this is present.
  4. // Via http://vayu.dk/highlighting-wp_nav_menu-ancestor-children-custom-post-types/
  5.  
  6. add_filter('nav_menu_css_class', 'current_type_nav_class', 10, 2);
  7. function current_type_nav_class($classes, $item) {
  8. // Get post_type for this post
  9. $post_type = get_query_var('post_type');
  10.  
  11. // Removes current_page_parent class from blog menu item
  12. if ( get_post_type() == $post_type )
  13. $classes = array_filter($classes, "get_current_value" );
  14.  
  15. // Go to Menus and add a menu class named: {custom-post-type}-menu-item
  16. // This adds a current_page_parent class to the parent menu item
  17. if( in_array( $post_type.'-menu-item', $classes ) )
  18. array_push($classes, 'current_page_parent');
  19.  
  20. return $classes;
  21. }
  22. function get_current_value( $element ) {
  23. return ( $element != "current_page_parent" );
  24. }
  25. ?>

Report this snippet  

You need to login to post a comment.