Last updated August 8, 2009. Created by joachim on August 8, 2009.
Log in to edit this page.

Theming terms

Both the List and Custom display options use theme functions, theme_term_display_list and theme_term_display_custom respectively.

The Load display option places data into the $node object, from which your theme can retrieve it, format it, and place it anywhere in the node template.

The format is as follows:

    [term_display] => Array
        (
            [7] => Array // the vocabulary ID
                (
                    [vocabulary_name] =>  // the vocabulary name, run through a check_plain
                    [terms] => Array   // an array of terms, in the same format as the $node->taxonomy array.
                )

The array of terms is an array of objects, each keyed by its term id (tid):

   [taxonomy] => Array
        (
            [56] => stdClass Object
                (
                    [tid] => 56
                    [vid] => 2
                    [name] => NAME
                    [description] =>
                    [weight] => 0
                )

See below for examples of what can be done with this.

Theming examples

The following examples of code should go in your theme's template.php, in either _phptemplate_variables()'s node case (Drupal 5) or theme_preprocess_node (Drupal 6). These will add an extra variable to be used in your node.tpl file.

Inline links

These are the same as the default terms.

  $vid = MY_VID; // fill in here
  if ($vars['node']->term_display[$vid]) {
    foreach ($vars['node']->term_display[$vid]['terms'] as $tid => $term) {
      $term_links['taxonomy_term_'. $term->tid] = array(
        'title' => $term->name,
        'href' => taxonomy_term_path($term),
        'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description))
      );
    }
    $vars['my_vocab'] = theme('links', $term_links, array('class' => 'links inline'));
    // $my_vocab is now available to node.tpl
  }

Definition list by vocabulary

This puts all loaded vocabs into a single definition list, with the vocabulary name. With some CSS formatting, this can make a nice block of meta data; extra data can be added.

      foreach ($vars['node']->term_display as $vid => $vocab_data) {
        $meta_dl["vocab_$vid"] = _mytheme_terms_dl_item($vocab_data);
      }
      // you can add extra DL items here
      $vars['meta_dl'] = '<dl>'. implode("\n", $meta_dl) .'</dl>';     
## and a helper function to place elsewhere in template.php:
/**
* Helper function to turn term_display loaded terms into DL items.
*/
function _mytheme_terms_dl_item($vocab_data) {
  $dt = $vocab_data['vocabulary_name'];
  foreach ($vocab_data['terms'] as $tid => $term) {
    $term_links['taxonomy_term_'. $term->tid] = array(
      'title' => $term->name,
      'href' => taxonomy_term_path($term),
      'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description))
    );
  }
  $dd = theme('links', $term_links, array('class' => 'links inline'));
  return "<dt>$dt</dt>\n<dd>$dd</dd>\n";
}

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.