Is there a way (besides adding a Custom Link) to add a custom post type archive to a menu in WordPress? If it’s added using a custom link (e.g. /cpt-archive-slug/), WordPress does not apply classes like current-menu-item
to the list element, which presents challenges when styling the menu.
If the custom link contains the entire URL (e.g. http://site.com/cpt-archive-slug/), those classes are added. However, that’s probably not a ‘best practice’.
your best opption is custom link with full url as Custom post types archives are different form taxonomy based archives (categories,tags,any custom taxonomy) and date based archives which have there own archive slug.
I know this is old but I have this problem too and I found a rather clean way to handle it is to use a custom menu walker
Having a custom link menu item with URL of
#post_type_album
, you can use it like this:Note: This assume that your post type’s slug and name are the same.
I have expanded a bit on tungd’s answer to provide more genericity with that approach. This implementation allows adding arbitrary mappings between menu ‘macros’ and internal WordPress URLs that only the backend knows about.
I’ve also decided to use
!
as the prefix for these macros to avoid them clashing with named anchors – this includes an overhead to strip off the ‘http://’ from the link URL (as WordPress will attempt to normalise these weird links). If that implementation bothers you, you can always remove thepreg_replace()
call and use#
as your link prefix as before.You can create a Page and then apply a custom archive template to it. I create and use archive-{post-type}.php fo the template name just like you would for the default archive, I just manually apply it to the Page.
If you do this, I’d recommend setting your CPT to has_archive=’false’ to avoid permalink collisions, regardless just make sure your Page has a different permalink slug than your CPT archive slug is set to.
I ran into the same exact problem, and found this solution:
Source:
http://www.wptavern.com/forum/plugins-hacks/2169-current-page-custom-post-type-archive.html
I think I’d have to create a new “parent” post-type, so the custom-post-type I want to show becomes a “child”, and that parent WILL be listed in the menu.
This should be an option. Creating a dummy parent just for getting it listed in the menu is too much work.
I’d like a way to do it from the template, or functions, so I don’t have to do it in each menu settings for each site in my multisite installation.
I Found this answer when searching for a similar issue. I just copied the code into my finctions.php file and it worked right out of the box 🙂 hope it helps!
Dynamic navigation for custom post type (pages)
What I do instead of adding the link is creating an empty page. In that page I add something like “Content generated from cpt-name” in the editor so my client knows where all the stuff is coming from.
Then I add that page to the menu and with the following code I ensure that is highlighted when archives-cpt or single-cpt is running.
This will check for menu/page title and if we are actually seeing the archive or individual page of a custom post type. If it’s true it will add the current-menu-item class.