I would like to know what is the best way to create a different template for parent and children categories and/or taxonomies.

Example: I have a taxonomy called region, which I want to divide in countries, and cities inside countries. So, I will have a parent taxonomy term called Italy, and it's children taxonomy terms, Rome, Milan, Napoli, whatever. Is it possible to have different templates for country and city? How can I achieve this? Thanks in advance.

2 Answers 2


I suggest creating 3 files

1) regiontemplate-country.php

2) regiontemplate-city.php

These 2 will contain the templates for country & city, then

3) taxonomy-region.php

In this file, add the code to load the appropriate template

$term = get_term_by('slug', get_query_var('term'), 'region');
    get_template_part('regiontemplate', 'city');
    get_template_part('regiontemplate', 'country');
  • 1
    This is a good idea, and it's going in the right direction. However, it always sends me to the 'country' file, so it's not recognizing the term parent, I suppose?
    – Cthulhu
    Commented Nov 2, 2012 at 11:27
  • most likely "term_id" is the wrong parameter, i'll do a print_r($wp_query->query_vars); to see the correct parameter & update this answer, give me 2 minutes Commented Nov 2, 2012 at 11:39
  • 2
    I used $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); and it worked like a charm. Thanks for your help.
    – Cthulhu
    Commented Nov 2, 2012 at 11:41
  • updated the answer too Commented Nov 2, 2012 at 11:43
  • 1
    get_term_by() is what you want instead...
    – Adam
    Commented Nov 2, 2012 at 11:43

WordPress has a template hierarchy which will look for particular template files in a specific order. If it exists, WordPress will use that template file to render your output.

For taxonomies, the specific hierarchy is as follows;

1. taxonomy-{taxonomy}-{slug}.php //e.g. taxonomy-region-milan.php
2. taxonomy-{taxonomy}.php //e.g. taxonomy-region.php
3. taxonomy.php
4. archive.php
5. index.php 

So taking the above into account you can create one template file for your region,

taxonomy-region.php //e.g. http://www.example.com/region/

...and a slug specific template file for each region,

taxonomy-region-milan.php //e.g. http://www.example.com/region/milan/
taxonomy-region-roma.php //e.g. http://www.example.com/region/roma/

What happens is this...

If a user navigates to,


WordPress will first attempt to locate taxonomy-region-milan.php, if found, it shall return this file as the template file for that taxonomy term. If not found, it shall move onto the next logical choice taxonomy-region.php as a fallback and so on working its way through the hierarchy list above until it arrives at index.php which you will most definitely have because without it, your theme wouldn't function anyway.

Now overall this concept might be suitable if you are working with only a few cities or regions within Italy or any country. But the moment you start to work with multiple cities/regions/country and find that you are constantly repeating the same code in different template files then its time to utilize a more efficient approach.

So my question to you is, will the template for all of the cities/regions as mentioned in your OP be the same? Or will each one vary differently? If so - how much? Because wherever possible you want to reduce repetitious code which makes your life easier if you ever need to adjust/edit/tweak something and have that change reflect itself through-out your site.

Manually doing this over multiple files can be tedious and leave you prone to more errors.

Resource: http://codex.wordpress.org/Template_Hierarchy

  • Thank you for your answer. However, I'm looking for a way to create different templates for country and city. This means that, when my client adds a new country or city, it should appear on the website using its associated template automatically. I don't think that creating a specific template file for each city would be a good idea, since the files would be all the same anyway. Maybe I should have explained myself better in the question, so I apologize.
    – Cthulhu
    Commented Nov 2, 2012 at 11:30
  • @Cthulhu As I suspected would be the case, however as you did not stipulate, I've shown the rudimentary example which is "ok" for light, infrequent use. However in your instance, Mridul examples how to do it more dynmically with get_template_part
    – Adam
    Commented Nov 2, 2012 at 11:42

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.