Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
We have tons of posts about this URL pattern. Please add your research efforts to your question. –  toscho Sep 11 '12 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 '12 at 4:59
add comment

1 Answer

up vote 0 down vote accepted

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?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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