description

These snippets allow you to insert Taxonomy images into your nodes, when using the taxonomy_image.module.

Before doing a lot of extra work for yourself, there are several Taxonomy Image add-ons distributed with the package and that virtually eliminate all this extra work.

usage

  1. Requires the taxonomy_image.module to be installed and enabled
  2. Use a text editor like notepad.exe or equivalent to copy and paste the snippets below into your template.php file and node.tpl.php (or node-type.tpl.php) files respectively
  3. Upload your edited your template.php file and node.tpl.php (or node-type.tpl.php) files to your active theme folder.

step 1 of 2

To override the default taxonomy_image layout copy the following snippet, using an editor like notepad.exe, name it TEMPLATE.PHP and upload it to your active theme folder.

If you already have a TEMPLATE.PHP file in your active theme folder, simply add the above to the existing TEMPLATE.PHP file and upload it.

NOTE: This is not to be confused with the template.php file in the /THEMES/ENGINES/PHPTEMPLATE folder. themes/engines/phpTemplate.

<?php
function _phptemplate_variables($hook, $vars) {
  if ($hook == 'node') {
    if (module_exist("taxonomy_image")) {
       foreach (taxonomy_node_get_terms($vars['node']->nid) as $term) {
        $vars['taxonomy_images'][] = taxonomy_image_display($term->tid, "alt='$term->name'");
       }
    }
  }
return $vars;
}
?>

step 2 of 2

Copy the following snippet into your your node.tpl.php or node-type.tpl.php file, where you want the taxonomy images to display. Edit the class names and adjust the layout to suit.

<div class="taximage">
<?php print $taxonomy_images[0] ?>
</div>

Comments

ellanylea’s picture

This is the starting point for helping me set up taxonomy_image. Having read tons of posts/comments, I put together a Summary Guide to taxonomy_image

-------------------------------------------------
AdAstra.ca - Optimze the Web. Optimze the World.

Z2222’s picture

The code didn't work for me in Drupal 5 until I changed module_exist to module_exists:

<?php
function _phptemplate_variables($hook, $vars) {
  if ($hook == 'node') {
    if (module_exists("taxonomy_image")) {
       foreach (taxonomy_node_get_terms($vars['node']->nid) as $term) {
        $vars['taxonomy_images'][] = taxonomy_image_display($term->tid, "alt='$term->name'");
       }
    }
  }
return $vars;
}
?>

Does anyone know how to insert taxonomy images into the page template (page.tpl.php) and not just the node templates?

Fennex’s picture

I dunno how great of a solution this is but you can try this (in drupal 5).

function _phptemplate_variables($hook, $vars) {
  if ($hook == 'node') {
	  if (module_exists("taxonomy_image")) {
       foreach (taxonomy_node_get_terms($vars['node']->nid) as $term) {
	    $GLOBALS['taximages'][$vars['node']->nid]=taxonomy_image_display($term->tid, "alt='$term->name'");
        $vars['taxonomy_images'][] = taxonomy_image_display($term->tid, "alt='$term->name'");
       }
    }
 }
return $vars;
}

and then in the page.tpl.php file use print $GLOBALS['taximages'][1] or something similar like

 foreach ($GLOBALS['taximages'] as $image){
 print $image;
 break;
 }

it should probably involve some if exists type logic...

HansKuiters’s picture

You can also change this:
if ($hook == 'node') {
to:
if ($hook == 'page') {
to display the images in pageview.

stephencarr’s picture

How about cutting out step 1 and just adding this to node.tpl.php :

<div class="categories">
<?php 
foreach (taxonomy_node_get_terms($node->nid) as $term) {
print taxonomy_image_display($term->tid, "alt='$term->name' title='$term->name'"); 
}?>
</div>

Works for me, displays all the images for any of the taxonomies that have images assigned to them.

tomaszx’s picture

in node.tpl.php

foreach ($node->taxonomy as $k => $v) {
    print taxonomy_image_display($k, "alt='$term->name'");
}
NancyDru’s picture

With the contributed add-ons to Taxonomy Image, virtually all theme modifications are unnecessary.

NancyDru (formerly Nancy W. until I got married to Drupal)

kingswoodute’s picture

Hi,

Can anyone point me in the direction of a good resource for displaying taxonomy images in nodes for Drupal 7?

I have only 1 vocabulary and every piece of content is allocated to one of 12 terms in that vocabulary.

Ideally what I'd like to achieve is to allow users to upload their own image or if they don't substitute the for the relevant taxonomy term.

Thanks heaps!

Jibus’s picture

Looking for the same thing