Created
          April 16, 2020 16:30 
        
      - 
      
- 
        Save ivo-ivanov/1c9d65987518c1238748814d29ed9b24 to your computer and use it in GitHub Desktop. 
    Menu highlight post type and custom page template. #wordpress
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2 ); | |
| function add_current_nav_class($classes, $item) { | |
| // Getting the current post | |
| global $post; | |
| $current_post_type = get_post_type($post->ID); // post, c45_project | |
| $item_page_id = get_post_meta( $item->ID, '_menu_item_object_id', true ); | |
| $template_name = get_page_template_slug($item_page_id); // template-projekte.php, template-news.php | |
| if (($current_post_type == 'post' && $template_name == 'template-news.php') || ($current_post_type == 'c45_project' && $template_name == 'template-projekte.php' )) { | |
| $classes[] = 'current-menu-item'; | |
| } | |
| // Return the corrected set of classes to be added to the menu item | |
| return $classes; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment