Taxonomy, Terms, and Template Files

I have post type ‘product’ and hierarchical taxonomy ‘types’ attached to it. In this taxonomy, I have terms: ‘dry-clean’, ‘washer’, etc. With several sub terms on each terms.

In my situation, I need to display, eg:

Read More
  1. http://example.com/types/washer -> display all sub terms on it
  2. http://example.com/types/washer/{subterm} -> display all posts on it

My questions is:

  1. How do I get permalink URL for parent terms? I try URL above, but it result in 404.
  2. How the template files work for that kind of problem?
    1. is taxonomy-types-washer.php enough for case #1? or should I create taxonomy-types.php and create the logic there?
    2. in my mind, after scanning template hierarchy, I might need taxonomy-types-{term_parent}.php to list all sub terms and taxonomy-types.php to list all product in sub terms.
    3. ultimately, to display each product, I will have to create single-product.php
  3. On a slightly unrelated problem. I find that archive-{posttype}.php doesn’t work when I don’t have any post on it. Is there any solution for this? (should I create different question, or stick with this one)?

UPDATE

I checked my rewrite_rules options and it doesn’t list [types] at all. I don’t know why. To test it, I change my slug into product-types, flush permalink and it list this:

[product-types/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?types=$matches[1]&feed=$matches[2]
[product-types/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?types=$matches[1]&feed=$matches[2]
[product-types/([^/]+)/page/?([0-9]{1,})/?$] => index.php?types=$matches[1]&paged=$matches[2]
[product-types/([^/]+)/?$] => index.php?types=$matches[1]

So I guess it’s registered now. I try to load URL product-types/washer it goes to 404. I try to load URL index.php?types=washer. It too, goes 404. Right now, I have this files:

  • taxonomy-types-washer.php
  • taxonomy-types.php

So, I don’t know what’s wrong with this one :(.

UPDATE #2

I find the problem. It’s because I missed 'rewrite'=>array('hierarchical'=>true)

Here’s the new rewrite_rules:

[product-types/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?types=$matches[1]&feed=$matches[2]
[product-types/(.+?)/(feed|rdf|rss|rss2|atom)/?$] => index.php?types=$matches[1]&feed=$matches[2]
[product-types/(.+?)/page/?([0-9]{1,})/?$] => index.php?types=$matches[1]&paged=$matches[2]
[product-types/(.+?)/?$] => index.php?types=$matches[1]

Related posts

Leave a Reply

1 comment

  1. These types of URLs are supported since WP 3.1:

    register_taxonomy( 'types', 'post', array(
      'hierarchical' => true,
      'rewrite' => array( 'hierarchical' => true ),
      ...
    );
    

    Remember to flush the rewrite rules after making the change.

    The template you would use for both parent and child terms is taxonomy-types.php:

    $current_term = get_queried_object();
    
    if ( 0 == $current_term->parent ) {
      // it's a parent term; display it's child terms using wp_list_categories() etc.
    } else {
      // it's a child term; display the posts using The Loop etc.
    }