Search code examples
phpdrupaldrupal-7drupal-taxonomy

Custom Taxonomy Term page in Drupal 7


I'm trying to make a custom Taxonomy Term page in Drupal 7. I've created a page--taxonomy.tpl.php file in my templates folder. The file only prints out a message. I now try to force the template file by adding

function template_preprocess_page($variables) {
  if (arg(0) == 'taxonomy') {
    $variables['template_file'] = 'page--taxonomy-tpl';
  }
}

in my template.php, but it won't work. Can you help me? And if I get the custom page working, how do I fetch the nodes with this term (in page--taxonomy.tpl.php)? Thanks in advance.


Solution

  • Try using this in your template.php:

    function template_preprocess_page(&$variables) {
      if (arg(0) == 'taxonomy') {
        $variables['theme_hook_suggestions'][] = 'page__taxonomy';
      }
    }
    
    • You need to pass $variables by reference, so add a & before it
    • template_file has changed to theme_hook_suggestions in Drupal 7
    • You don't need the -tpl in the template suggestion unless you want it to be a part of the filename like "page--taxonomy-tpl.tpl.php" which I don't think is what you want.

    For more information, check out template_preprocess_page(), theme_get_suggestions() and Working with template suggestions