Hi,

How do I access taxonomy terms array in page.tpl.php? The $node variable and $terms variable is only accesible in node.tpl.php. I need to get access to this variable so i can add a class to the body tag.

thanks

Comments

patoshi’s picture

Hi,

Here is your answer:

<pre><?php print_r($node); ?></pre>

You can use that to view the entire $node array on the page.tpl.php. Put right under the body tag to have it displayed then select what var you want to use after that.

take care.

nevets’s picture

$node is available in page.tpl.php if viewing a single node in which case $node->taxonomy is an array of taxonomy terms.

motoservo’s picture

There may be a better solution but I had a similar problem (I wanted an ID on each body) and I solved it. My suggestion is:

In your template.php (assuming you're using Drupal 6) write out the following loop:

function YourTemplateName_preprocess_page(&$variables)
{
	foreach($variables['node']->taxonomy AS $tax_term)
	{
		$variables['body_class'] = strtolower($tax_term->name);
	}
}

Then, in your page.tlp.php you can reference the variable as $body_class. I know this will work with a single taxonomy term but you will need to declare $body_class as an array if you want to use mutiple taxonomy terms. That I haven't tested.

summit’s picture

Hi,

Anyone have altered this code please to use multiple taxonomy terms?
Could I also than chooses a taxonomy term per vocabulary?

greetings,
Martijn

mths’s picture

I'm using zen for theming and wanted to add the taxonomy terms to the body classes, I used above function, but altered it thus:

function YourTemplateName_preprocess_page(&$vars)
{
    foreach($vars['node']->taxonomy AS $tax_term)
    {
        $vars['body_classes'] .= " ".strtolower($tax_term->name);
    }
}

This would also add all the terms as classes.
I'm unsure where your $variables comes from, isn't $vars the right one? At least in the Zen theme it is and Garland is using it as well. (where in the api is documentation on this?)

cduwe’s picture

Thanks for the code snippets. The original is working for me but the version for multiple terms isn't. And both snippets generate the following message: warning: Invalid argument supplied for foreach() in /home/.../template.php on line 225.

Any ideas what could be going wrong with the argument?

When I use the version for multiple terms I'm not getting taxonomy terms back but page variables like "not_front"

Any assistance would be greatly appreciated

lasseitorp’s picture

But it still works as such??

Anybody??

KD

nevets’s picture

The code assumes the node has taxonomy terms, you can re-write as

<?php
function YourTemplateName_preprocess_page(&$vars)
{
  if ( is_array($vars['node']->taxonomy) ) {
    foreach($vars['node']->taxonomy AS $tax_term)
    {
        $vars['body_classes'] .= " ".strtolower($tax_term->name);
    }
  }
}
?>

to avoid the errors.

Wolfgang Reszel’s picture

How to do this in Drupal 7?

steinmb’s picture

Drupal 6.22 and Zen 6.x-2.1 subtheme
The above code does not work on my system, this does. Note that this code uses the termID, then term-name could easy contain spaces or get changed by the admin/user.

<?php
function theme_preprocess_page(&$vars, $hook) {
  if ( is_array($vars['node']->taxonomy) ) {
    foreach($vars['node']->taxonomy AS $tax_term)
    {
      $vars['classes_array'][] .= ' tid-' . ($tax_term->tid);
    }
  }
}
?>

@steinmb

nevets’s picture

theme_preprocess_page() should be YOURTHEMENAME_preprocess_page()