If you’ve ever added a custom link to a WordPress menu item that directs to a custom post type archive, you’ve probably been dismayed at the fact that the highlighting classes such as “current_page_item” aren’t added for you and even worse, “current_page_parent” is added to the menu item for the page that displays your recent blog posts. Here’s a quick snippet that will help you get the desired class structure:

7 thoughts on "Highlight Menu Item for Custom Post Types"

  1. Paul says:

    I usually added a custom class to the menu item, and used it in conjunction with the body class:
    .event-archive .events

    1. thdadmin says:

      That’s one solution, but doing something like this allows you to not rely on settings within WordPress since it’s controlled by the theme or plugin this code is in.

  2. demu says:

    Hi Tammy, great stuff! I’m learning a lot from your tuturials. I’ve included this code in my plugin but $item->url returns as string consisting of all the url’s of the menu pasted together, so the ‘current_page_item’ class is not added.
    Sorry for the noob question, teaching myself as I go along.
    best,
    Demu

  3. Andrew says:

    This is great. I am, however, looking for something that also covers taxonomy archive pages, and this does not do that. Any ideas?
    Cheers,
    Andrew

    1. thdadmin says:

      So basically, this asks if we’re current on a page that is either a custom post type or its archive. Next it removes the “current” class from the item. Then it says, but hey, if the current menu item is a URL to the custom post type’s slug, add that “current” class back in.

      So what you need to do is change the conditional from a post type to a taxonomy and make sure the slug you’re looking for is the one for the taxonomy.

      1. Jaco says:

        Thank you ! Works perfect with my custom taxonomies 😉

  4. Amy CQ says:

    Worked perfectly! Thank you! P.S. Why haven’t they fixed this in the core? You’d think it’s a bugaboo that has many frustrated WordPressers scratching their heads! :/

Leave a Reply

Your email address will not be published. Required fields are marked *