1

Below is how the URL layout is for my site using a Custom Post type called book and a custom tag taxonomy called book_tag

I am un-happy with the way Wordpress is writing the URL for my custom tag taxonomy. Below is an example of how my new post type and taxonomy are currently writing the URLs and how I would like it to do it.

Custom Post Type Name: book
Custom Taxonomy Name/Type: book_tag

Viewing the Index for my custom post type...

website.com/{custom-post-type}/

Viewing a custom post type single page...

website.com/{custom-post-type}/{post-name} 

Viewing a Custom Tag Taxonomy that is Linked for use on my Custom Post Type

website.com/{custom-taxonomy-type}/{taxonomy-name} 

I would like for the custom taxonomy to look more like it is under the custom post type like this...

website.com/{custom-post-type}/{custom-taxonomy-type}/{taxonomy-name} 

or even better...

website.com/{custom-post-type}/tag/{taxonomy-name}  

So currently a tag with the slug "css" is being wrote like this...

http://website.com/book-tag/css/  

I would like it to be more like this...

http://website.com/book/book-tag/css/ or http://website.com/book/tag/css/ 

So my question, is this possible? To make the URL structure show a custom taxonomy to be UNDER a custom post type?

2
  • We have tons of posts about this URL pattern. Please add your research efforts to your question.
    – fuxia
    Sep 11, 2012 at 4:47
  • @toscho I have added an answer below, I am not sure how to call this for best performance though
    – JasonDavis
    Sep 11, 2012 at 4:59

1 Answer 1

0

After reading through about 20+ post on here, this is what I have come up with...

In this example http://test.com/{custom-post-type}/tag/{custom_taxonomy_name}/
or
http://test.com/book/tag/css/

Would do the trick I am after.

//http://test.com/book/tag/css/
function custom_taxonomies_rewrite(){
    add_rewrite_rule('^book/tag/([^/]*)/?','index.php?book_tag=$matches[1]','top');
}
add_action('init','custom_taxonomies_rewrite');

I am not sure on the best way to call this though, right now it is called with init action. This seems like something that should only be called once and then the re-writes file rebuilt.

If this looks like the right way to do this, please tell me where I should be calling this code from, with init or another action/hook?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.