While the following text describes theme changes, the add-on modules that are distributed with Taxonomy Image are designed to eliminate the need to make most theme changes. Please check them out before modifying theme code.

Update your theme or other php code to display taxonomy images. To display a taxonomy image from your theme or other php code, add a call to the taxonomy_image_display function. This function requires that you pass the term id of the term for which you wish to display the appropriate image. For example: taxonomy_image_display($term->tid).

This will return an <img> tag with the appropriate values for "src=," "height=," "width=," and "alt=." For example, it may return something like: <img src="files/image.jpg" width="75" height="75" alt="term-name" title="term-description">. If there is no associated image, it will return NULL (unless you are using "Recursive image display" and the term's parent has an associated image).

If you wish to set other IMG attributes, Taxonomy_image_display may be called with an associative (indexed) array of attributes. If an attribute is the same as one of the computed attributes (width, height, alt, title), the supplied attribute will be used.

For example, taxonomy_image_display($term->tid, array('border' => '2', 'hspace' => '5')

This would then return the following "IMG" tag: <img src="files/image.jpg" width="75" height="75" alt="term-name" title="term-description" border="2" hspace="5" />

See the included theme patches (in the 'contributed' folder) for more examples.

How Can I Use Taxonomy Image?


Modify Your Theme

To actually display images from your theme, you will have to modify the theme to make a call to taxonomy_image_display. When calling this function, you will need to pass in the taxonomy term for which an image should be displayed. For example, here is a functioning modification to the Garland node.tpl.php.:

<?php
  echo '<div id="node-'. $node->nid .'" class="node'. ($sticky ? ' sticky' : NULL) . ($status ? NULL : ' node-unpublished') .'">';
  echo $picture;

  if ($page == 0) {
    echo '<h2><a href="'. $node_url .'" title="'. $title .'">'. $title .'</a></h2>';
  }

  if ($submitted) {
    echo '<span class="submitted">'. $submitted .'</span>';
  }

  echo '<div class="content clear-block" style="background:#ffe8e8;">'. $content .'</div>';

  echo '<div class="clear-block"><div class="meta">';
  if ($taxonomy) {
    $output = NULL;
    foreach ($node->taxonomy as $term) {
      if ($image = taxonomy_image_display($term->tid)) {  
        $output .= "$image";
      }
    }
    echo $output;  
  }
  echo '</div>';

  if ($links) {
    echo '<div class="links">'. $links .'</div>';
  }
  echo '</div>';
  echo '</div>';


Taxonomy Term Links as Pictures

If you'd like to replace the taxonomy term links that display with your content by pictures, you can use hook_link_alter to change the link text into IMG tags.

Note: if you want to do this, you should either scale the images down, or load small images to start with. A reasonable maximum size for images used as links is about 64 pixels on each side. If they are bigger than this, they will dominate the display.

function mymodule_link_alter(&$node, &$links) {
  foreach ($links AS $module => $link) {
    if (strstr($module, 'taxonomy_term')) {
      $tid = substr($module, 14);
      $term = taxonomy_get_term($tid);
      $tname = $term->name;
      // See if we have a taxonomy image associated with this term
      $taxo_image = taxonomy_image_display($term->tid);
      if ($taxo_image) {
        $links[$module]['title'] = $taxo_image;
        $links[$module]['html'] = TRUE;
      } /* end image insertion */
    } /* end if taxonomy_term */
  } /* end foreach */
}


See the next page for a better example and great news for non-coders.

Prefix a Teaser View with an Image

Another commonly sought use is to prefix a teaser view with an image. This can be done in the hook_nodeapi(op=view) function.

function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($node->type != 'pinkslip') { return; }   /* don't do any others */

  if ($op == 'view') {
    $termlist = taxonomy_node_get_terms($node->nid);
    $terms = array();

    foreach ($termlist as $key => $term) {
      $taxo_image = taxonomy_image_display($term->tid, "border='0', hspace='5', vspace='5'")
      if ($taxo_image) {
        // Quit when we find the first one
        break;
      }
    }

   // Prepend the teaser with the IMG tag
    $node->content['teaser']['#value'] = $taxo_image . $node->content['teaser']['#value']
  }
}