Skip to content

Instantly share code, notes, and snippets.

@ivo-ivanov
Created April 16, 2020 16:30
Show Gist options
  • Save ivo-ivanov/1c9d65987518c1238748814d29ed9b24 to your computer and use it in GitHub Desktop.
Save ivo-ivanov/1c9d65987518c1238748814d29ed9b24 to your computer and use it in GitHub Desktop.
Menu highlight post type and custom page template. #wordpress
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