tags:

views:

19

answers:

2

In Drupal 6, how do you print a taxonomy term as a CSS body class?

I have found this snippet that lets you print almost every aspect of Drupal content as a body class, but it doesn't include taxonomy terms:

http://www.davidnewkerk.com/book/122

Being able to print taxonomy terms as a body class is essential for theming processes, so I am surprised that a solution is not readily available.

A: 

Check what variables are passed to the page template by either doing print_r($vars) or dpm($vars) in your page pre-process function or using the http://drupal.org/project/devel_themer module. The usage of dpm require you to install the devel module.

You will find that some themes will pass $taxonomy as a variable to page.tpl.php . If that is not the case you can find the taxonomy terms in the $node variable which is also available in the page.tpl.php in some themes.

(The above holds true for my fusion based theme acquia marina http://drupal.org/project/acquia_marina ). Once you have these taxonomy terms available you can easily print them out in your body classes.

Sid NoParrots
A: 

After much hard work, I found a very easy way to do this.

On Drupal Snippets, there is a snippet that lets you print out the taxonomy terms applied to each page as text.

The only problem is that the snippet will print any spaces or punctuation that are in the taxonmy term, which is no good for body classess.

However, by adding a str_replace command, you can strip out all the spaces and punctuation.

I'm sure there are other people who wants to print taxonmy terms as body classes, so to save them the bother, here is the code that I used with the str_replace command added.

Put the following in template.php:

function getTerm($label, $vid, $link) {
    $node = node_load(array('nid'=>arg(1)));
    foreach((array)$node->taxonomy as $term){
        if ($term->vid == $vid){
            if ($link){
                $link_set[] = l($term->name, taxonomy_term_path($term));
            } else {
                $link_set[] = $term->name;
            }
        }
    }
    if (!empty($link_set)){
        $label = ($label) ? "<strong>$label </strong>" : "";
        $link_set = $label.implode(', ', $link_set);
    }
   $link_set = str_replace(' ', '_', $link_set);
  $link_set = str_replace('&', 'and', $link_set); 
  $link_set = strtolower($link_set);

 return $link_set;
}

Put the following in Page.tpl.php:

<body class="taxonomy-<? print getTerm(false, 1, false);?>">

I hope this helps anyone who has the same problem.

Extra tips: (1)In the code I have posted, the only punctuation that is striped out is the ampersand (i.e. '&'). If you have other punctuation to strip out use the following:

$link_set = str_replace('INSET_PUNCTUATION_HERE', 'INSERT_REPLACEMENT_HERE', $link_set);

Place this command under the other $link_set lines in the code I have posted for template.php.

(2) In the page.tpl.php code I have posted, the "taxonomy-" part places the words taxonomy and a dash before each body class term. You can edit this as you wish to get the results your require.

big_smile