I have searched at length to find how to use taxo image in page.tpl.php as a page header as opposed to in node.tpl.php. The best thing I could find was http://drupal.org/node/44922#comment-223646

Except that the foreach in this code creates a huge amount of errors in the logs. I see a lot of other people asking how to do this as well. Could you possibly provide some documentation, or even a snippet of code for this? If I can get it working I am more than happy to write up documentation and a complete step-by-step process for others.

Thanks.

Comments

Bevan’s picture

Yes, documentation on this would be useful. Here is an implementation I recently did;

The following code goes in template.php, and will probably need to be adapted to fit around existing code in the _phptemplate_variables() function. It could go in either the page of node case. It depends on taxonomy_image_get_url() which I introduced in this patch http://drupal.org/node/164448 which at time of writing I've only just submitted, so you'll have to apply it manually for this code to actually work -- at time of writing

define('VID_WITH_TAX_IMAGES', 2); // Change the number to reflect the vid of the vocabulary containing the taxonomy with taxonomy images you wish to use.

function _phptemplate_variables($hook, $vars = array()) {  
  switch ($hook) {
    case 'page':

      // Find the taxonomy terms in channels vocab
      if (!empty($vars['node']->taxonomy)) {
        foreach ($vars['node']->taxonomy as $tid => $term) {
          if ($term->vid == VID_WITH_TAX_IMAGES && is_numeric($tid)) {
            $vars['channels'][strtolower($term->name)] = $term;
            $vars['taxonomy_image_css'] = ' style="background-image:url(\''. taxonomy_image_get_url($tid) .'\');"';
          }
        }
      }

      break;
  }
}

You'll also need to put something like

<div id="header"<?php print $taxonomy_image_css ?>>

in your page.tpl file for this to become part of the theme.

I'll describe a more generalized implementation here that you could use to base documentation on:

Find template.php in your theme directory. If it doesn't exist, create it (there is documentation on this that you can link to).
In template.php, find function _phptemplate_variables(). If it doesn't exist in your template.php file, create it by copying and pasting this code (I think there's already documentation on this too):

function _phptemplate_variables($hook, $vars = array()) {  
  switch ($hook) {
    case 'page':

      /* We're going to insert code here */

      break;
  }
}

Now you have to decide if you want your taxonomy image to be used in node.tpl.php, or in page.tpl.php. In this example I've used it in page.tpl.php, but you can use node.tpl.php instead. Once you've decided, look for the 'page' case in _phptemplate_variables() (or the 'node' case if you want to use node.tpl.php). If you copied the above code, you can change case 'page': into case 'node': .

Inside this case we're going to set $vars['taxonomy_image'], which will become $taxonomy_image in page.tpl.php (or node.tpl.php). So start by putting print $taxonomy_image; somewhere in page.tpl.php (there is documentation on this you can link to).

Now in template.php in the case 'page', add this code

if(!empty($vars['node']->taxonomy)) { 
  foreach($vars['node']->taxonomy as $tid => $term) {
    $image = taxonomy_image_display($tid);
    if(!empty($image)) {
      $vars['taxonomy_image'] = $image;
      break; // stop the foreach loop after the first term with an image.  You can delete this line to use the last term instead
    }
  }
}

The following example is useful if you want to have multiple images. You'll have to use $taxonomy_images instead of $taxonomy_image in your tpl file.

if(!empty($vars['node']->taxonomy)) { 
  $vars['taxonomy_images'] = ''; // Declare / reset
  foreach($vars['node']->taxonomy as $tid => $term) {
    $image = taxonomy_image_display($tid);
    if(!empty($image)) {
      $vars['taxonomy_images'] .= $image;
    }
  }
}

I hope that's helpful. Let me know if/when it gets into documentation and/or the handbook so I can check it out! :)

By the way, to stop the errors in the code you linked to, wrap the foreach block in an if statement like this:

if(!empty($some_vars)) { 
  foreach($some_vars as $a_var) {
    /* ... */
  }
}
nancydru’s picture

Status: Active » Closed (duplicate)
muka’s picture

Version: 4.7.x-1.x-dev » 5.x-1.x-dev

Another cohice could be with the taxonomy_context module http://drupal.org/project/taxonomy_context

ex. in my page.tpl.php




if($taxonomy_context = @taxonomy_context_get_context()){ // you got the module and a context

	if($image_context = taxonomy_image_get_object($taxonomy_context->tid)){
	
		$new_css = "#logo { background:transparent url({$image_context->url}) no-repeat scroll right top; }";
		$styles .= "<style media=\"all\" type=\"text/css\">$new_css</style>"; //dirty way..
		//dvm($image_context);
	
	
	}// image_context
}// taxonomy_context



I think this snippets could be a bit improved..

Thanks and compliments for the great job

Bye, luca

nancydru’s picture

Thanks, Luca. Interesting concept.