Inserting Taxonomy Images into nodes

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>

Summary Guide to taxonomy_image

AdAstra - December 5, 2006 - 09:21

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.

Drupal5 taxonomy image

J. Cohen - March 28, 2007 - 18:07

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?

taxonomy image in the page template

chicablanca - April 19, 2007 - 22:24

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

<?php
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

<?php
print $GLOBALS['taximages'][1]
?>
or something similar like

<?php

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

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

You can also change

capono - July 24, 2007 - 10:26

You can also change this:

<?php
if ($hook == 'node') {
?>

to:
<?php
if ($hook == 'page') {
?>

to display the images in pageview.

Simpler than the rest

stephencarr - January 14, 2008 - 11:07

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.

for Drupal 6 you use

tomaszx - March 28, 2008 - 13:15

in node.tpl.php

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

Probably don't need this

NancyDru - September 3, 2008 - 22:22

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

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

 
 

Drupal is a registered trademark of Dries Buytaert.