Another example is, where I can filter class but this doesn’t work with multiple post types.
It always adds active class to first item:
// Remove active class from menu
function remove_active_class($class) {
return ( $class == 'active' ) ? FALSE : TRUE;
}
// Add active class to menu of post type single template
function add_class_to_wp_nav_menu($classes) {
switch ( get_post_type() ) {
case 'portfolio':
$classes = array_filter( $classes, 'remove_active_class' );
if( in_array( 'menu-portfolio', $classes) ) {
$classes[] = 'active';
}
}
return $classes;
}
add_filter('nav_menu_css_class', 'add_class_to_wp_nav_menu');
I think I’m suffering from the same problem. Whenever I add a new custom post type to my navigation menu, both it and the nav item for my main posts page get the .active class. I’ve tried Ben’s code and that worked for me but it’s not very scalable/elegant if I have to keep adding my custom post types to that switch statement. Anyone have a better solution?
I ignore the parent/child hierarchy determined by the pages menu (which is affected by a WordPress bug) and instead rely only on the menu hierarchy. It’s easily done by replacing the roots_nav_menu_css_class with the following:
This is nicer but it only half fixes the issue for me because when I’m on a single custom post page the nav item for that post type is missing the .current_page_parent class so it’s not getting picked up by the filter.
BTW, I really appreciate all the input guys, I’m picking this stuff up as I go along!
I’ve been working on a fix to this but it needs some testing. It would be great if you could implement the changes in this branch and let me know how you fare.
It’s a long time since this topic but, did somebody found a new solution that not implies to edit the function for every custom post type? Something more like ‘drag&drop’ function. This wordpress ‘bug’ with the archives and menus is really annoying and has remained in wordpress updates, I can’t understand why. Anyway, roots snippets solutions works well, but I was curious about new approaches or solutions you probably have found guys. Thank you!
Hey all I’m also experiencing the same issue with a CPT archive page. I tried overwriting my nav.php with the /nav_rejig's nav.php but apparently there’s no function definition for roots_url_compare() and that causes a fatal error. I’d love to be able to implement your elegant solution instead of a janky switch statement search + replace or something. Please let me know how to use the new code and sorry if I’ve overlooked the proper way to use it.
Lots of changes since I created that branch, but I imagine the fix still works. The function you are looking for should be in utils.php on the same branch.
Thanks @Foxaii! For some reason github’s search wasn’t finding that func definition when I looked for it before…
Also, just in case anyone else is trying to implement this, you’ll also need to include the relative-urls.php file in /lib to get it to work. Looking forward to 8.0!
I tried doing what @caliper suggested and now on the pages based on CPT’s, I end up with blank pages with a 500 error returned from the server when I look at the console. Not sure what’s happening.