Created
July 31, 2020 10:42
-
-
Save sarvar/4bb34833e581e216f5a6fc44cd37b2a3 to your computer and use it in GitHub Desktop.
Highlight custom post type parent as active item in Wordpress Navigation
This file contains 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
function remove_parent($var) | |
{ | |
if ($var == 'current_page_parent' || $var == 'current-menu-item' || $var == 'current-page-ancestor') { return false; } | |
return true; | |
} | |
function tg_add_class_to_menu($classes) | |
{ | |
if (is_singular('projects')) | |
{ | |
$classes = array_filter($classes, "remove_parent"); | |
if (in_array('menu-item-194', $classes)) $classes[] = 'current-page-ancestor'; | |
} | |
elseif (is_singular('blog')) | |
{ | |
$classes = array_filter($classes, "remove_parent"); | |
if (in_array('menu-item-196', $classes)) $classes[] = 'current-page-ancestor'; | |
} | |
elseif (is_singular('library')) | |
{ | |
$classes = array_filter($classes, "remove_parent"); | |
if (in_array('menu-item-195', $classes)) $classes[] = 'current-page-ancestor'; | |
} | |
elseif (is_singular('travel')) | |
{ | |
$classes = array_filter($classes, "remove_parent"); | |
if (in_array('menu-item-192', $classes)) $classes[] = 'current-page-ancestor'; | |
} | |
return $classes; | |
} | |
if (!is_admin()) { add_filter('nav_menu_css_class', 'tg_add_class_to_menu'); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment