Custom Post Type Archives in WordPress Menus?
I was setting up a development WordPress environment under a non-root directory on a remote server this morning. Created a couple of custom post types and, as usual, was about to create separate pages for them (more on the technique later). This usually works out quite well, however, I decided to take another well-known route – use their existing archive pages via their slugs. Went over to the Menu and, obviously, found no Custom Post Type archive metabox that I could create a Menu item from. Custom Post Type posts – sure. Custom Taxonomies? Yes, Sir. Custom Post Type archives? …
And, of course, I couldn’t take the thought off my mind all day (probably won’t sleep tonight figuring out). How does one set up a proper archive page menu item? There are, generally, two approaches to this (+1 hackish one). If you know of more, do share, please.
This is the most straight forward and popular approach. No coding required. Plop in the Custom Link and she’s ready to sail, Captain. However, remember how I had a non-root directory? So in order for this to work out, the link had to contain the sub-directory. And, knowing that it will change once I move the site into production… doesn’t seem right. The dump would have to be additionally
sedded for the sub-directory, or the links manually edited after moving. Not right at all, doesn’t feel… “WordPress” enough.
Since most of the time a Custom Post Type would have a distinct archive template nevertheless, why not create a Page with a distinct template and go from there? The template would have a custom Loop inside, the rest works as well as an archive theme page. The page can be added to the Menu in the regular way and will stay there no matter what happens. Many will argue against this approach. Be it incorrect theme, template and object semantics or simply too long-winded and improper. Agreed, not the best approach either.
Page slug collisions
This hack depends on a collision of slugs between those of a Page and those of an Archive. You can (but should probably not) create a Page with the same slug as a Custom Post Type Archive slug (shouldn’t this be impossible?), and, due to archive rewrite rules being positioned higher up in the rewrite lookup than page slugs. Dabbling in the realm of rewrites is beyond the scope of this article; this article is what you’d look at instead. Anyways, this is highly unreliable as rewrite order may change in the future and Page slugs may take over. But good to know of, nevertheless.
I encourage you to share your comments and custom solutions. Will probably provide some concept code tomorrow, so stay tuned and thanks for reading.
As promised, a follow-up can be found here: Custom Post Type Archives in WordPress Menus – because we can!.