How can I theme the taxonomy/term/x page?

Dan Albey picture Dan Albey · Apr 16, 2010 · Viewed 22.1k times · Source

Although it was easy to find some info online about how to theme other stuff (e.g. search results), it is impossible to find a straightforward article about how to theme the output of a taxonomy/term/247 page?

How can I do it?

Answer

sepehr picture sepehr · Apr 18, 2010

In Drupal 6, you can make use of node-taxonomy.tpl.php and page-taxonomy-term.tpl.php files in your theme to template taxonomy pages considering that the second one is the wrapper for the first. Behave node-taxonomy.tpl.php like node.tpl.php and page-taxonomy-term.tpl.php like page.tpl.php. for example:

page-taxonomy-term.tpl.php

<?php require 'header.tpl.php'; ?>
    <body class="<?php echo $body_classes; ?>">
        <div id="page">
            <?php require 'page-navigation.tpl.php'; ?>
            <div id="main">
        <h2>Taxonomy term page</h2>
        <div class="taxonomy-content">
            <?php if ($tabs): echo '<div id="tabs-wrapper" class="clear-block">'; endif; ?>
            <?php if ($title && !$node): echo '<h2'. ($tabs ? ' class="with-tabs"' : '') .'>'. $title .'</h2>'; endif; ?>
            <?php if ($tabs): echo '<ul class="tabs primary">'. $tabs .'</ul></div>'; endif; ?>
            <?php if ($tabs2): echo '<ul class="tabs secondary">'. $tabs2 .'</ul>'; endif; ?>
            <?php if ($show_messages && $messages){ echo $messages; } ?>
            <?php echo $help; ?>
            <?php echo $content; // contains the output of node-taxonomy.php, that's why I call this wrapper template file. ?>
        </div> <!-- #taxonomy-content -->                   
            </div> <!-- #main -->           
        </div> <!-- #page -->
        <?php echo $closure; ?>
    </body>
<?php require 'page-footer.tpl.php'; ?>

node-taxonomy.tpl.php

<div id="node-<?php echo $node->nid; ?>" class="node<?php if($sticky) echo ' sticky'; ?><?php if(!$status) echo ' node-unpublished'; ?>">
  <div class="taxonomy-node">
      <div class="node-body">
        <a class="node-title" href="<?php echo $node_url ?>" title="<?php echo $title ?>">
          <?php echo $title ?>
        </a>
        <span class="node-cck-field">
          <?php echo $node->field_cck_blah[0]['view']; ?>
        </span>                             
      </div>                
  </div>
</div>

Well, the most important part: By default the node-taxonomy.tpl.php is not known to Drupal, so we need to introduce this as a template suggestion in the our theme's template.php file, here we go:

/**
 * Adding custom PHPTemplate suggestions on taxanomy pages.
 *
 * @param $vars
 *   A sequential array of variables to pass to theme template.
 */
function phptemplate_preprocess_node(&$vars) {
  if(arg(0) == 'taxonomy'){
    $suggestions = array('node-taxonomy');
    $vars['template_files'] = array_merge($vars['template_files'], $suggestions);
  }
}

Also there is a taxonomy-term.tpl.php, regarding Drupal 7.
It's a code sample, dont' forget to use check_plain() & check_url() on printouts.