0

I would like to use a custom template for my taxonomy term pages. I attempted doing so by creating a hook in template.php like so

function mythemename_preprocess_page(&$variables){
    if($hook == 'node'){
        if(arg(0) == 'taxonomy'){
            $vars['template_file'] == 'node-taxonomy';
        }
    }
}

And then creating a custom template called node-taxonomy.tpl.php

However the page does not change.

I understand that I can achieve something similar using views, but since other sites are using different themes on our drupal instance I'd prefer not to change anything that relies on those views.

  • Your PHP code is incorrect in multiple places. What are you trying to do - provide custom templates for node.tpl.php or page.tpl.php on taxonomy pages? – Aram Boyajyan Mar 25 '16 at 20:07
  • Yes that is the intention – Matt Mar 25 '16 at 20:08
1

Theme hook suggestions are made based on these factors, listed from the most specific template to the least. Drupal will use the most specific template it finds:

  • taxonomy-term--tid.tpl.php
  • taxonomy-term--vocabulary-machine-name.tpl.php
  • taxonomy-term.tpl.php

Note that underscores in a vocabulary's machine name are replaced by hyphens.

  • Is there a way to specify a template for a specific taxonomy term view mode without display suite? I used hook_entity_info_alter to add a view mode to taxonomy terms. Now i need to assign a template to that view mode? – oll Jun 19 '17 at 13:59
1

In addition and for those who need, you can provide template suggestions for taxonomy terms that way :

function hook_theme_suggestions_taxonomy_term_alter(&$suggestions, $vars, $hook) {
  $term = $vars['elements']['#taxonomy_term'];
  $sanitized_view_mode = strtr($vars['elements']['#view_mode'], '.', '_');
  $suggestions[] = 'taxonomy_term__' . $term->bundle() . '__' . $sanitized_view_mode;
  $suggestions[] = 'taxonomy_term__' . $term->id() . '__' . $sanitized_view_mode;
  $suggestions[] = 'taxonomy_term__' . $sanitized_view_mode;

  return $suggestions;
}

It will provide, e.g. :

"taxonomy_term__vocabularyname__teaser"
"taxonomy_term__257__teaser"
"taxonomy_term__teaser"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.