Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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