Using Custom Post Type as 'Page' – Menu highlights both CPT AND 'Blog'

Hi all,
I searched high and low for a solution to this problem of ‘blog’ being highlighted in the menu in addidtion to my CPT menu item.

Not sure if anyone else has already tackled this, but I’ll post here.

I found this gem on GitHub:

I put these bits:

// Get post_type for this post
$post_type = get_query_var('post_type');

// Removes current_page_parent class from blog menu item
if ( get_post_type() == $post_type )
  $classes = array_filter($classes, "get_current_value" );

into the top of the “roots_nav_menu_css_class” function in roots/lib/nav.php

And added the “get_current_value” function above the “roots_nav_menu_css_class” function.

The rest of the “roots_nav_menu_css_class” function remains untouched.

This works for me now, but I’m not sure if there is a cleaner way to do it, e.g. adding it to roots’s custom.php as an override.

I’m really interested in hearing feedback on this, or if anyone handled this issue in another way.


Have you seen the Roots snippet on the subject?


Awesome! I had not seen that. It’s a shame this didn’t turn up when I searched.
I’ve dropped that into my custom.php and it works great.

Any reason why the ‘if’ criteria has been left as a manual edit?

I’ve used Vayu’s:

$post_type = get_query_var('post_type');
if ($post_type == get_post_type()) {

Is there any reason why that might be a bad idea?

Thanks again! I wish I had started my search here.

Actually, I think I’ve spoken too soon. I had not tested thoroughly. I’m getting better results with code I had posted originally. Could be due to the naming and rewrite setup of my CPT. Seems to be more or less the same in principle though, just less slim than Ben’s snippet.