[wp-hackers] Hierarchical Taxonomies permalinks

The WordPress Web Warlock wordpress at web-warlocks.net
Tue Jun 15 00:33:22 UTC 2010


En/na Ken Newman ha escrit:
> Working with a custom Hierarchical Taxonomy, I've noticed that the 
> permastructure does not append the parent term to the permalink.
>
> example.com/taxonomy/parent/
> example.com/taxonomy/child/
>
> In a default post category permalink, the parent is appended.
>
> example.com/category/parent/
> example.com/category/parent/child/
>
> Anyone have a way to make custom taxonomies behave more like categories?
>
I do just the opposite: make (some) categories behave more like custom 
taxonomies. This way, I can use the wonderful category__and parameter of 
get/query_posts (all I got with taxonomies was just a single rewrite, so 
querying two different taxonomy terms at once was a real headache). (How 
I do this exactly is, methinks, out of the scope of this question, but 
if s/o is interested in the details, mail me or whatever).

For this specific problem of yours, I create a Page in the desired 
location, which has a template just like category.php (actually, it's 
the very same category.php, with a header to identify it as Page 
template, and a selector to retrieve query_var('cat') or a custom field 
in the Page, whatever applies). Also, some metadata needs to be added to 
the category; I use a custom termmeta table, but that can be saved as an 
option, just like parents.
As a final step, hook to get_term_link, and return the Page permalink 
instead of the default category permalink, if the category happens to 
link to a Page.

So, you can have
example.com/taxonomy/parent/child

... but if you wanted, you could even have...

example.com/some-taxonomy-wrapper/taxonomy/parent/grandson/third-degree-grandson/subset-additional-page

... that is, add some higher-level all-taxonomy wrapper category (useful 
if, say, you have "system" taxonomies like content-type, and "content" 
taxonomies like "foo", "bar", "barring foo", etc.), skip some children 
or "flatten" some levels (you could have movies/index/t/things-to-come  
"flattened" as movies/things-to-come , and also movies/index and 
movies/index/a), &c.

Anyway, maybe this trick can do for you. Create a Page template that 
reads some custom field/whatever to link it to the term, and hook to 
get_term_link: that could do the job.


More information about the wp-hackers mailing list