Last active
November 7, 2016 12:16
-
-
Save ozrabal/506e19868fe677b83372c2d54541b5ab to your computer and use it in GitHub Desktop.
Highlight parent menu item when displayed single post from cpt archive
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
<?php | |
add_filter( 'nav_menu_css_class', 'pwp_cpt_current_item_class', 10, 2 ); | |
function pwp_cpt_current_item_class( $classes , $item ){ | |
$type = get_post_type_object(get_post_type()); | |
if(!empty($type->rewrite['slug'])) { | |
if (get_post_type() == $type->name || is_archive($type->rewrite['slug'])) { | |
$classes = str_replace('current_page_parent', '', $classes); | |
if ($item->url == '/'.$type->rewrite['slug']) { | |
$classes = str_replace('menu-item', 'menu-item current_page_item', $classes); | |
remove_filter('nav_menu_css_class', 'pwp_cpt_current_item_class', 10, 2); | |
} | |
} | |
} | |
return $classes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment