WordPress: Getting a menu item selected on a Custom Post Type

I am by no means a WordPress expert. Quite the opposite in fact. While learning the ropes, I look at WordPress with a frustrating blend of admiration and confusion. I’m so impressed with both Custom Post Types and the Navigation System┬ábut I can’t get the two to co-operate. I’m not sure if I’m misunderstanding something, or it’s a limitation of WordPress, but when I view a post (of type “Custom Post Type”) the custom menu item I’ve added doesn’t have the current-menu-item class added.

Menus are awesome, except in the way I want to use them

I think I’m suffering from a limitation of WordPress’ navigation menus. You can only create menu items which link to pages, posts or “custom”. If I want to link to the archive index of my custom post type (let’s call it “/products”) then I simply hard code a URL.

Here’s the limitation: There’s no metadata to go with that menu item which says “by the way, this menu item relates to a specified custom post type”. The best WordPress can do would be to match the URL in the browser to the menu item’s URL and add the class “current-menu-item”. That’s all built in and that works (once of the things I like about WordPress). However, if you go to “/products/apples” (to view the “apple” post of type “product”) then the post URL doesn’t match the custom URL on the menu item and so no class is added to the menu item.

My very cheap solution

Given that there’s no meta data on the menu item which hints at the custom post type, I’m left with very circumstantial things to work with. My “solution” is that if the menu item’s label matches the name of the custom post type, that should be the current menu item. So here’s the work around I created.

Like I said – I’m not a WordPress expert. I imagine there are some seasoned WP devs out there pinching the bridges of their noses in despair. However, if I’ve explained myself correctly, you’ll understand the problem I’m having. Please, get in touch and let me know what I’m doing wrong.

Tagged with: , ,
Posted in PHP, Wordpress

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">