Add class to link to custom post type archive page in WordPress menus.
<?php
function ccd_cpt_archive_menu_filter( $items, $menu, $args ) {
foreach ( $items as &$item ) {
if ( $item->type != 'custom' ) continue;
if ( get_query_var( 'post_type' ) == 'your-cpt-one' && $item->title == 'CPT One' ) {
$item->classes []= 'your-class';}
else if ( get_query_var( 'post_type' ) == 'your-cpt-two' && $item->title == 'CPT Two' ) {
$item->classes []= 'try_current_page_item';}
else if ( get_query_var( 'post_type' ) == 'your-cpt-three' && $item->title == 'CPT Three' ) {
$item->classes []= 'or--something-else';}
}
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'ccd_cpt_archive_menu_filter', 10, 3 );
?>
WordPress Snippet