Different templates for parent and children categories/taxonomies

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.

Related posts

Leave a Reply

2 comments

  1. 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

    <?php
    $term = get_term_by('slug', get_query_var('term'), 'region');
    if((int)$term->parent)
        get_template_part('regiontemplate', 'city');
    else
        get_template_part('regiontemplate', 'country');
    
  2. 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/
    etc...
    

    What happens is this…

    If a user navigates to,

    http://www.example.com/region/milan/
    

    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