Templates for Custom Post Types and Custom Taxonomies

You have totally missed the naming convention when coming to the taxonomy archive pages, and most probably the same goes for your archive pages for your custom post types

Here is how your taxonomy archive pages should look like

  • taxonomy-{taxonomy}-{term}.php – If the taxonomy were sometax, and taxonomy’s term were someterm WordPress would look for taxonomy-sometax-someterm.php. In the case of Post Formats, the taxonomy is post_format and the terms are post_format-{format}. i.e. taxonomy-post_format-post-format-link.php

  • taxonomy-{taxonomy}.php – If the taxonomy were sometax, WordPress would look for taxonomy-sometax.php

  • taxonomy.php

  • archive.php

  • index.php

So, all your taxonomy templates should be called taxonomy-tax_{$name_lower}.php where $name_lower is the name assigned to the variable.

Another issue that I brought up in my comments to your posts was

Do not use hyphens in your names, make use of underscores to separate words

This is now the opportunity to test your names with and without hyphens and see how that affect how your templates are used.

Example:

If you make your taxonomy name tax-mytax and you create a template taxonomy-tax-mytax.php, you will notice that this will not work, as wordpress reads your template as follow: tax is your taxonomy name and mytax is a term

One last thing, and I don’t know if you intentionally left that out, but your function should be hooked to the init hook

EDIT 1

Just also another point on good practice, write your arguments in English and make them translatable. Someone that speaks a language different from yours will have a tough time figuring out what the labels mean, as would be the case with me :-). This is also the one big reason why your function should be hooked to init as to make the translators available

EDIT 2

It seems that your rewrite rules is causing your issue. From what was discussed in chat,

I have a taxonomy named tax_evenimente. It is a category-like taxonomy. I have a term Alte Evenimente with the slug alte-evenimente. site.com/evenimente/alte-evenimente should be displayed with taxonomy.php.

Yes, that is what should happen with your rewrite rule, but it keeps on 404ing.

<—SECTION SCRAPPED—>

EDIT 3

I have found a great plugin to help with the rewrite rules for your custom post types. It is called Custom Post Type Permalinks (NOTE: I have no affiliation to the plugin). As your code currently stands, leave it in place

Here is how everything works:

(For the sake of examples, I’m going to use cpt_evenimente and tax_evenimente)

  • Download and install the plugin

  • Go to your permalinks settings page and scroll down to the settings for your custom post types. This is how it will look

enter image description here

  • Change /%postname%/ to /%year%/%monthnum%/%postname%/ to get your desired URL

  • Save your permalinks

Now, for the tests

  • site.com/evenimente/alte-evenimente/ Displays taxonomy archive for terms from taxonomy tax_evenimente on taxonomy-tax_evenimente.php

  • site.com/evenimente/ Displays the custom post type archive for custom post type cpt_evenimente

  • site.com/evenimente/2014/10/testing-posts-1/ Displays the single post testing post 1

Just a note, the checkbox for “Use custom permalink of custom taxonomy archive” should be unchecked

I hope this is what you need.

Leave a Comment

tech