WordPress Template for Post Type with Nested Taxonomy

This article assumes you have a decent understanding of:

  • WordPress Template Hierarchy
  • Registering Custom Post Types
  • Registering Custom Taxonomies
  • Attaching Taxonomies to Post Types

Let’s say you have a Custom Post Type of “Portfolio”, with a rewrite slug of portfolio. So the URL on the frontend is: domain.com/portfolio/.

Let’s also say you have a Custom Taxonomy of “Categories”, with a rewrite slug of portfolio/category (attached to your Custom Post Type of “Portfolio”). So the URL on the frontend is: domain.com/portfolio/category/<term-slug>/

With the standard WordPress Template Structure and functionality…

  1. Upon visiting: domain.com/portfolio/ the Post Type Archive template will display just fine.
  2. Upon visiting: domain.com/portfolio/category/<term-slug>/ the Taxonomy Term Archive template displays just fine.
  3. Upon visiting: domain.com/portfolio/category/ the Taxonomy template doesn’t work. Resulting in a 404 Not Found error.

I’m a believer in all “paths” in the URL structure should resolve to a valid page with relevant content.

  • Referring to #1 above, that can be handled in archive-$posttype.php in the WordPress Template Hierarchy.
  • Referring to #2 above, that can be handled in taxonomy.php in the WordPress Template Hierarchy.
  • Referring to #3 above, that’s not handled at all. There’s no template file in the WordPress Template Hierarchy to handle a “Taxonomy view”.

taxonomy.php in the WordPress Template Hierarchy refers more to the currently viewed “Term” within the “Taxonomy”. Which to me… Is kind of misleading. The default taxonomy.php should in my opinion, actually be term.php.

Note: By default, there’s no such thing as a term.php template file in WordPress.

I’ve come up with a few tweaks which allow your WordPress theme to have a term.php AND taxonomy.php in the template structure of your WordPress theme.

  • term.php will handle the currently viewed Term within a Taxonomy. This is basically replacing the default functionality of taxonomy.php template file, and will serve as a “central hub” for all Post Objects assigned to a given Term Object.
  • taxonomy.php will handle the currently viewed Taxonomy. Basically redefining it’s default intended purpose, and will serve as a “central hub” for all Term Objects in a given Taxonomy.

This will allow you to have a template for the Taxonomy itself, and another template for Terms in a Taxonomy.

Disclaimer:

I believe I have come up with a temporary solution from random miscellaneous sources online.

I would love for someone to expand on my answer to make it more “bullet proof” so to speak. However, in the short run, this solution DOES work.


WordPress Template Files For Taxonomy Archives and Term Archives

Step 1 – Modify the Current Rewrite Rules Used by WordPress

1.1) You need to add some new rewrite rules to your existing rewrite rules currently in-use by WordPress.

 

1.2) Generate new rewrite rules… (I think this area needs some improvement for sure — I’m not savvy with rewrite rules.)

There’s really only one rule that I found is absolutely necessary to make this work. I could be missing other important rewrite rules, but this is the one NECESSARY rewrite rule to make this adjustment to the template structure.

By adding the new rewrite rule, WordPress no longer sees domain.com/portfolio/category/ as a 404, but as a Post Type Archive instead.

There’s more work to be done. You can’t leave this URL as a Post Type Archive, because then you will have a conflicting template structure with domain.com/portfolio/ and will result in unexpected behavior.

Step 2 – Modify the Post Type Archive Template File

You need to modify the behavior of the Post Type archive template file to conditionally load the Taxonomy template file instead. This is what’s triggered on domain.com/portfolio/category/

Now if you go to domain.com/portfolio/category/ it loads taxonomy.php from your currently active WordPress theme directory.

Step 3 – Modify the Taxonomy Template File

You need to modify the behavior of the Taxonomy archive template file to conditionally load the Term archive template file instead. This is what’s triggered on domain.com/portfolio/category/<term-slug>/

Now if you go to domain.com/portfolio/category/<term-slug>/ it loads term.php from your currently active WordPress theme directory.


Conclusion

  1. When viewing domain.com/portfolio/ – Default archive templates are loaded as usual. (archive.php, archive-$posttype.php, etc) from your currently active WordPress theme.
  2. When viewing domain.com/portfolio/category/ – The taxonomy.php template file is loaded from your currently active WordPress theme.
  3. When viewing domain.com/portfolio/category/<term-slug> – The term.php template file is loaded from your currently active WordPress theme.

You can easily test to see if this works. All you need is three files in your currently active WordPress theme directory.

  1. archive.php or archive-posttype.php
  2. taxonomy.php
  3. term.php

Put this in archive.php or archive-$posttype.php

Put this in taxonomy.php

Put this in term.php

 


Credits

Original question asked by @Michael Ecklund on WordPress Stack Exchange: Custom Post Type with Nested Taxonomy and Template Files.
Answer to Question: Custom Post Type with Nested Taxonomy and Template Files by @Michael Ecklund on WordPress Stack Exchange.

WordPress Stack Exchange

If you have a question related to WordPress, you can asked it on WordPress Stack Exchange and someone will very likely provide an answer or solution to your question or issue.

Related Questions

This article assists in answering all of the following related questions:

  1. [WordPress Development] Custom Post Type with Nested Taxonomy and Template Files asked by: @Michael Ecklund
  2. [WordPress Development] How to modify URL structures in custom post types and taxonomies or terms asked by: @Michael Ecklund
  3. [WordPress Development] Custom post types and permalink asked by: @theplayer777
  4. [WordPress Development] How to change permalink structure for custom post type and it’s taxonomies? asked by: @Mark
  5. [WordPress Development] How to show custom taxonomy in the permalink? asked by: @Lou
  6. [WordPress Development] Change Permalink Structure for Custom Taxonomy asked by: @Saurabh Goel
  7. [WordPress Development] Create permalink structure for one taxonomy archive per custom post type asked by: @jpsingleton
  8. [WordPress Development] Custom Permalink Structure for Custom Post Type, Custom Taxonomy, Under a page asked by: @61Pixels
  9. [WordPress Development] Custom permalink with child taxonomy terms asked by: @Shane
  10. [WordPress Development] Custom Permalinks for Custom Taxonomies/Post Types -> Top Level Pages not working asked by: @luke
  11. [WordPress Development] Custom Post Type and Custom Taxonomy Permalinks asked by: @Rynoceris
  12. [WordPress Development] Custom post type and taxonomy permalinks – Structure asked by: @Lucas Yarid
  13. [WordPress Development] custom post type archive template per custom taxonomy term asked by: @gfaw
  14. [WordPress Development] Custom post type taxonomies URL rewrite asked by: @Jaquis
  15. [WordPress Development] Custom Post Type with two hierarchical Custom Taxonomies: strategy to generate best permalink structure asked by: @Lopinsjk
  16. [WordPress Development] Custom post type, permalinks, taxonomies and blog posts asked by: @T G
  17. [WordPress Development] Taxonomy, Terms, and Template Files asked by: @ariefbayu
  18. [WordPress Development] Custom post types, taxonomies, and permalinks asked by: @RodeoRamsey
  19. [WordPress Development] Which template name to use for taxonomies but not their terms? asked by: @Maikal
  20. [WordPress Development] Custom taxonomy returns 404 asked by: @mihajloWR
  21. [WordPress Development] Custom Taxonomy specific to a Custom Post type asked by: @Saurabh Goel
  22. [WordPress Development] Define permalinks for custom post type by taxonomy in WP 3.4 by: @reekogi
  23. [WordPress Development] How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name asked by: @Jeff
  24. [WordPress Development] How to setup a Custom Taxonomy Term template asked by: @NM6
  25. [WordPress Development] Need some templating advice for templating with custom post types and taxonomies asked by: @user3905
  26. [WordPress Development] permalink /category/post-name with custom post type and taxonomy asked by: @Owl
  27. [WordPress Development] Permalink structure CPT with custom taxonomy fails asked by: @NielsPilon
  28. [WordPress Development] Rewriting a custom-post-type permalink with taxonomy term? asked by: @mathiregister
  29. [WordPress Development] Shouldn’t this be easy?! Custom post type/custom taxonomy permalink asked by: @fxfuture
  30. [WordPress Development] taxonomy term names in custom post type permalink structure asked by: @wppatrickk
  31. [WordPress Development] Template files for Custom Post Type and Taxonomy asked by: @Paul D
  32. [WordPress Development] Template for Custom Post Type and its Category asked by: @Seyed
  33. [WordPress Development] Templates for Custom Post Types and Custom Taxonomies asked by: @aifrim
  34. [WordPress Development] Permalink structure between 2 custom post types and a taxonomy asked by: @Max Lutz
  35. [WordPress Development] WordPress Custom Permalink Structure with Custom Post Type and Taxonomies asked by: @gleenk

 

2 Replies to “WordPress Template for Post Type with Nested Taxonomy”

    1. If you’re referring to the type hinting in my functions, you can eliminate the type hint, and all should work fine. I use PHP 7 and it works fine for me.

      If you’re referring to the callback functions with the WordPress hooks… and you wanted to add them as a class method instead of a function, then you would need to change the callback to something like: add_filter( 'some-hook', array( $this, 'some_method' ) );

Leave a Reply