Inserting Taxonomy Images into nodes
description
These snippets allow you to insert Taxonomy images into your nodes, when using the taxonomy_image.module.
usage
- Requires the taxonomy_image.module to be installed and enabled
- 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
- 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
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
The code didn't work for me in Drupal 5 until I changed
module_existtomodule_exists:<?phpfunction _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
I dunno how great of a solution this is but you can try this (in drupal 5).
<?phpfunction _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
<?phpprint $GLOBALS['taximages'][1]
?>
<?php
foreach ($GLOBALS['taximages'] as $image){
print $image;
break;
}
?>
it should probably involve some if exists type logic...
You can also change
You can also change this:
<?phpif ($hook == 'node') {
?>
to:
<?phpif ($hook == 'page') {
?>
to display the images in pageview.
Simpler than the rest
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
in node.tpl.php
foreach ($node->taxonomy as $k => $v) {print taxonomy_image_display($k, "alt='$term->name'");
}