How to Create Separate Drupal Templates For Parent and Child Taxonomy Terms?

325 Views Asked by At

I have a complex taxonomy hierarchy for product categories.

Every main taxonomy term has at least one child term. Some of those child terms have child terms. So in some places the taxonomy tree is one level deep and in other places it is two levels deep.

I need to create two separate Drupal templates for the taxonomy terms depending on the level.

I have already created a custom template for the top level using a view.

I am trying to figure out how to dynamically render the taxonomy term using a custom template depending on whether it is in level 1 down or level 2 down.

I am guessing I will need to use a template pre-processing function?

Is that the best way to approach this?

1

There are 1 best solutions below

0
On

You can try adding a relationship to the parent term (in the display's advanced settings) Then add a filter (filter by by term name or termID) and set it to use the parent relationship.

Limit the displayed terms to:

  1. terms where the parent term is null (to get the base term)
  2. terms where the parent term is not null (to get terms with at least one parent)