Skip to content

Instantly share code, notes, and snippets.

@doubleedesign
Created January 3, 2021 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doubleedesign/3d6853907d5efc2bb4273b6b6cb40e0b to your computer and use it in GitHub Desktop.
Save doubleedesign/3d6853907d5efc2bb4273b6b6cb40e0b to your computer and use it in GitHub Desktop.
Add class to CPT archive link in menus when viewing a post of that type
<?php
// Add current-menu-item to post type archive link for this post's type
function doublee_menu_classes($classes, $item) {
global $post;
$id = (isset($post->ID) ? get_the_ID() : NULL);
if(isset($id) && $item->type == 'post_type_archive') {
$current_post_type = get_post_type($id);
$link_post_type = $item->object;
if($current_post_type == $link_post_type) {
$classes[] = 'current-menu-item';
}
}
return $classes;
}
add_filter('nav_menu_css_class', 'doublee_menu_classes', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment