2

I'm using this snippet for marking the parent navigation active when on custom post type single page:

add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2 );  
function add_current_nav_class($classes, $item) {

    // Getting the current post details
    global $post;

    // 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;

}

But it doesn't mark a page when you are one more step inside that page.

For example: When I'm at home/photo, the active link marks with .current-menu-item with no problem. But, when I'm inside home/photo/this-photo, the active link class disappears.

Here's is my site. To replicate the behavior, please click on one of the thumbnails to go to a single page from that post type and see that photography link on the left deactivates.

This is also replicable inside blog page. This is a custom template with a custom loop to get all the posts (not custom posts).

Here's my code:

add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2 );

function add_current_nav_class($classes, $item) {

  // Getting the current post details
  global $post;

  // 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;

}
3
  • can you echo the $item->url and the $menu_slug for home/photo/this-photo, maybe even var_dump the $current_post_type to see more details? Commented Jul 29, 2016 at 7:04
  • You might want to add the class active. Commented Jul 29, 2016 at 7:06
  • hey websupporter, that did it! Thanks. I added active on the classes. But it only did it in the custom post types. Not inside the blog. Commented Jul 29, 2016 at 7:10

1 Answer 1

2

to highlight the menu element, you should add the class active. This will work for your photo custom post type.

Concerning your blog posts: As you can see, in the URL of a blog post there is no post/ which would be necessary for your code to act on. Furthermore, the blog overview page does not equal the custom post rewrite slug.

You could use something like this statement to add the active class for blog posts:

if ( 
     'post' === get_post_type($post->ID) 
     && false !== strpos( $menu_slug, 'blog' )
) {
    $classes[] = 'current-menu-item';
    $classes[] = 'active';
}
2
  • Hey, I'm still having problem. Why am I getting an active state on the third menu item? (web development link) Commented Jul 29, 2016 at 10:32
  • Ok, I fixed it by putting an if statement if ( ! is_home() ) before $classess and it worked. It's weird though. Commented Jul 29, 2016 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.