Can't decide between $taxonomy_images[0] or [1]?

Last modified: May 23, 2007 - 12:08

I got up to $taxonomy_images[3]!

I’ve been having fun with Taxonomy Image and, building on all the documentation out there, I think I’ve found a way that works consistently in all the test cases I’ve thrown at it.

You should be able to;–

  • Associate multiple images with a node, using multiple terms
  • Style images in teasers differently to those in a full node

There are a few important differences, so I thought I should document them…

Of course, you will need Taxonomy and Taxonomy Image, installed, enabled and accessible (documented elsewhere).

There are two files that we are interested in; these can be in themes/engines/phptemplate/ or over-ridden by versions in your theme.

template.php

You’ll probably need to create this file in your theme.

<?php
function _phptemplate_variables($hook, $vars) {
  if (
$hook == 'node' && module_exists("taxonomy_image")) {
    foreach (
taxonomy_node_get_terms($vars['node']->nid) as $term) {
      if (
$img = taxonomy_image_display($term->tid, "title='$term->description'")) {
       
$vars['taxonomy_images'][] = "<a href='files/category_pictures/$term->name.jpg'>" . $img . '</a>';
      }
    }
  }
  return
$vars;
}
?>

This will produce an <img> tag inside an anchor pointing at the source.  You could point it elsewhere, include a paragraph containing the description (I’ve put it in the title) or calm down, and continue with the rest of this article.

node.tpl.php

You’ll probably need to copy this file to your theme and edit it there.

This will place the tags where you want them in the node.

Finally, you can style with…

style.css

.taxonomy_image{
  float: right;
  margin: 10px;
}
/* images in teaser */
.taxonomy_image img{
   width: 100px;
  height: 100px;
}
/* images in full node */
.breadcrumb~.node .taxonomy_image img{
   width: 180px;
  height: 180px;
}
/* If anyone can preserve aspect ratio, please let me know. */

I’ve been using Drupal for just a few weeks, so if I’ve made any incorrect assumptions, do let me know.

 
 

Drupal is a registered trademark of Dries Buytaert.