How to get parent menu highlighted for custom post type

Can’t work out how to get a top level menu item highlighted.

I’ve created a custom post type “Projects” using ACF Pro. The “Post Type Key” is “projects” and it’s using a custom permalink : the “URL Slug” is “project” and the “Archive Slug” is “projects”.

The menu item is a “Custom Link” and the “URL” is “/projects”. This displays the projects archive as expected and the menu item is highlighted. Clicking into a project shows an individidual project as expected.

The issue is that the top level nav item “Projects” is not highlighted as the parent when showing an individual project.

The menu item that DOES get highlighted with the css class “current_page_parent” is in fact the News menu item. In the menu this is “Posts Page” and works as expected showing the News archive or an individual News post. News is the standard WP blog entry.

What do I need to do to change the behaviour so that the top level navigation “Projects” gets the css class “current_page_parent” when a single “Project” is displayed?

Hello @FourCandles,

Thank you for the inquiry.

This is the expected behavior since there is no hierarchical connection between the custom link /projects and the project item. You can try this filter in the functions.php file to manually add the class name current_page_parent to the custom link:

   add_filter( 'nav_menu_css_class', function( $classes, $item, $args, $depth ) {
    if ( is_singular('projects') || is_post_type_archive('projects') ) {
        if ( $item->url === '/projects' ) {
            $classes[] = 'current_page_parent';
        }
    }
    return $classes;
}, 10, 4 );

Did you set the Options > Enable Archives to “Yes”?

https://www.advancedcustomfields.com/blog/custom-post-types-without-plugin/#options-tab

Best regards.

Thank you for the prompt reply and pointers.

ACF Pro plugin has the “Archive” toggle switched on for the Project custom post type.

I used a modified version of the code for functions.php and changed the way News is served up using the new Masonry option in the Row, instead of an Archive page. All working now and I’ve learned something new!

Thank you for your help :smile:

Hey @FourCandles,

You’re most welcome!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.