I´m trying to create the following URL structure:
example.com/attractions <-- post type archive example.com/attractions/taxonomy_term <-- taxonomy archive example.com/attractions/van-gogh-museum <-- custom post type
Unfortunately this isn´t possible by default: WordPress needs the taxonomy base in the URL, as SE user sanchothefat wrote in his answer to this question:
...if you don't want the taxonomy-slug to be there then you're out of luck, you can't have the same slug as your post type or another taxonomy as it won't know what term is from which taxonomy and what's a post.
In other words, WordPress needs this permalink structure:
My question: Is there a way to make this work using WordPress rewrite API?
I suppose it involves 2 steps:
- Removing the taxonomy base from the URL
- Tell WordPress what post type it needs to display
I found a code snippet that should remove the taxonomy base from the URL:
global $wp_rewrite; $wp_rewrite->extra_permastructs['taxonomy_base'] = array('%taxonomy_base%', EP_NONE);
(Source - see last answer on bottom of page)
This isn´t the complete solution though, since WordPress can´t tell if the post is a taxonomy archive or a post type...
The author of this great article on Advanced Taxonomy Queries with Pretty URLs wrote me:
What you're asking isn't too difficult, but it could be a bit tricky. The rewrite rules need to know what type of thing the second term is(..) That's why the category/tag/taxonomy name prefix is prepended. You could create a rewrite so that everything after /attractions/ is treated as a taxonomy URL, but doing both gets a little tricky.
For anyone having the same issue, have a look at this discussion on WordPress Answers. Especially MikeSchinkel´s plugin code might help.
Here´s an interesting example of WPCandy combining the taxonomy base and term in the URL: