Last active
April 22, 2022 14:22
-
-
Save cr0ybot/6236d1fccf315ec5aa2af7580d3a348c to your computer and use it in GitHub Desktop.
Fix WordPress current_page_parent for Blog and Custom Post Type Archive Menu Links
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
/** | |
* Checks the current page to see if current_page_parent should be removed from the blog link or added to a cpt archive pagelink | |
*/ | |
function cr0ybot_cpt_menu_highlight( $classes, $item, $args ) { | |
// Remove current_page_parent from Blog if not on a post | |
$cpp = array_search( 'current_page_parent', $classes ); | |
if ( $cpp !== false && $item->type === 'post_type' ) { | |
$qo = get_queried_object(); | |
if ( ( $qo instanceof WP_Post && $qo->post_type !== 'post' ) || | |
$qo instanceof WP_Post_Type && $qo->name !== 'post' ) { | |
array_splice($classes, $cpp - 1, 1); | |
} | |
} | |
// Add current_page_parent for cpts | |
else { | |
$pt = get_post_type(); | |
if ( $pt !== 'page' && $item->object === $pt ) { | |
$classes[] = 'current_page_parent'; | |
} | |
} | |
return $classes; | |
} | |
add_filter( 'nav_menu_css_class', 'cr0ybot_cpt_menu_highlight', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment