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.
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
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;
So taking the above into account you can create one template file for your
region
,…and a slug specific template file for each region,
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 choicetaxonomy-region.php
as a fallback and so on working its way through the hierarchy list above until it arrives atindex.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