0

I'm trying to get a taxonomy archive template file to work, but with no luck. I think it might have to do with the fact that I declared a slug for the taxonomy like custom_post_type/taxonomy in order to keep the custom post type showing.

Here's how I register the custom post type and the custom taxonomy:

$args = array(
    'labels' => $labels,
    'query_var' => __('color', 'mydomain'),
    'rewrite' => array(
        'slug' => __('houses/color', 'mydomain'),
        'with_front' => true 
    )
);

register_taxonomy('color', null, $args);

$args = array(
    'labels' => $labels,
    'public' => true,
    'capability_type' => 'post',
    'supports' => array('title', 'thumbnail', 'custom-fields'),
    'has_archive' => true,
    'taxonomies' => ['color'],
    'rewrite' => array(
        'slug' => __('houses', 'mydomain'),
        'with_front' => true
    )
);

register_post_type('house', $args);

My template file is called taxonomy-color.php and I expect it to be used when the pages /houses/color/red and /houses/color/green are visited.

Note: my actual custom post type name contains a dash, I don't know if it matters.

3
  • Why would you register a taxonomy with a slash in the slug? Commented Feb 8, 2018 at 17:40
  • @LewisDonovan To make a specific permalink structure.
    – Howdy_McGee
    Commented Feb 8, 2018 at 18:23
  • As stated in the question, to be able to show the post type the taxonomy is assigned to. I didn't find any other way to do that, unfortunately. Commented Feb 8, 2018 at 20:36

1 Answer 1

0

You need to register the Custom Taxonomy BEFORE the Custom Post Type that it belongs to in order for the rewrite rule to work. The taxonomy needs to “exist” before the post type in order for WordPress to build the URL correctly.

Your Answer

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

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