WordPress: Maintaining Active Nav Status with Custom Post Types

Scenario: You have a custom post type archive page, and you want to be able to add some custom content to the archive without hardcoding it in the template. This is easy enough – you can create a page with the same name and slug, and set your templates to pull in the data from the page on the archive listing page by performing a query based on the post type slug.

However, it introduces a problem with the active state on the WordPress nav. You can create a custom nav item to point to the slug of the archive for your post type, but WP will see the nav item as referencing a page, even though you specified the item as custom.

In order to fix this behavior, you will need to filter the wp_get_nav_menu_items function to tell WP that the nav item is custom if it references a post type, which will force WP to match based on the URL and restore the active state to the nav:

Leave a Reply