Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save markhowellsmead/5aeb12903281f196af9678dc1cf87e20 to your computer and use it in GitHub Desktop.
Save markhowellsmead/5aeb12903281f196af9678dc1cf87e20 to your computer and use it in GitHub Desktop.
Mark (highlight) post type parent as active item in Wordpress Navigation. When you visit a custom post type's single page, the parent menu item (the post type archive) isn't marked as active. This code solves it by comparing the slug of the current post type with the navigation items, and adds a class accordingly.
<?php
add_action('nav_menu_css_class', function ($classes, $item) {
// Getting the current post details
$post = get_queried_object();
if (isset($post->post_type)) {
if ($post->post_type == 'post') {
$current_post_type_slug = get_permalink(get_option('page_for_posts'));
} else {
// Getting the post type of the current post
$current_post_type = get_post_type_object(get_post_type($post->ID));
$current_post_type_slug = $current_post_type->rewrite['slug'];
}
// Getting the URL of the menu item
$menu_slug = strtolower(trim($item->url));
// If the menu item URL contains the current post types slug add the current-menu-item class
if (strpos($menu_slug, $current_post_type_slug) !== false) {
$classes[] = 'current-menu-item';
}
}
// Return the corrected set of classes to be added to the menu item
return $classes;
}, 10, 2);
@2fernandez
Copy link

Hi Mark, thanks for sharing this it works perfectly well.
I was trying to output the title or name of the active menu item on a single page of a custom post type. I've tried just this:

	$menu_items = wp_get_nav_menu_items( 'main' );

	$this_item = current( wp_filter_object_list( $menu_items, array( 'object_id' => get_queried_object_id() ) ) );
	echo "active:" . $this_item->title;

But was not successful I was wondering if I could output anything from your bit of script that would let me do this.

Thanks for your help.

@markhowellsmead
Copy link
Author

Not sure, sorry. That's not what this piece of code is for.

@2fernandez
Copy link

2fernandez commented Jul 26, 2019

Cool thanks.
Yes, I understand the main function of this is to keeps the menu active.
But I was just wondering if there was a way to also display the title of that menu in the template.

@2fernandez
Copy link

Hi Mark,
just one more thing. Your code works very well thank you. It keeps menu items active even if I have Blog > posts selected. It however does not keep a menu item active if I set up a menu Blog Category > Post.

Ie: if I set up a blog category > the category is listed and then a post item is selected from this it tends not to work.

Are you aware of this ? do you have a work around by any chance ?

thanks again for your help.

@markhowellsmead
Copy link
Author

No, sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment