I'm building a custom theme in Drupal 7 which allows for the creations distinct main sections. Each section is comprised of pages that share common design elements (main banner, specific couloured gradient behind sub-navigation, colour of headlines and sub-heads, etc...). I was planning to use a taxonomy vocabulary called "Section" to allow author/editor users to create pages and assign them a section with a taxonomy drop down whenever a new page was added.

In Drupal 6 I would use THEME-NAME_preprocess_page(&$args) {} in template.php to grab the taxonomy term and add a variable that I could then use in my page.tpl.php to assign to the body tag however this doesn't seem to be possible and although I have tried many different methods of preprocessing, I can't seem to readily add any variables to my page.tpl.php or node.tpl.php.

I've discovered some of the changes in theming Drupal 7 and am happy to change my theming practices, I just need some direction and although the documentation indicates we can use a variable called $terms to grab Taxonomy within the template, that doesn't seem to exist in the scope of the page.

Any tips, direction, links for further ready would be greatly appreciated.

Thanks,
Andrew

EDIT Sept 4, 2011: This discussion weaves and bobs a bit so I thought for clarity I would clean it up a bit and put the solution I finally came up with in the initial post. Hopefully this is helpful.

* thanks to agoradesign for clarifying the use of the taxonomy_node_get_tems() function which I found in this thread.

The solution:

function taxonomy_node_get_terms($node, $key = 'tid') {
    static $terms;

    if (!isset($terms[$node->vid][$key])) {
        $query = db_select('taxonomy_index', 'r');
        $t_alias = $query->join('taxonomy_term_data', 't', 'r.tid = t.tid');
        $v_alias = $query->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
        $query->fields( $t_alias );
        $query->condition("r.nid", $node->nid);
        $result = $query->execute();
        $terms[$node->vid][$key] = array();
        foreach ($result as $term) {
            $terms[$node->vid][$key][$term->$key] = $term;
        }
    }
    return $terms[$node->vid][$key];
}

function MYTHEME_preprocess_html(&$variables) {
    if(arg(0)=='node' && is_numeric(arg(1))) {
        $node = node_load(arg(1)); 
        $results = taxonomy_node_get_terms($node);
        if(is_array($results)) {
            foreach ($results as $item) {
               $variables['classes_array'][] = "taxonomy-".strtolower(drupal_clean_css_identifier($item->name));
            }
       }
   }
}

** There is an extended version that allows you to choose the vocabulary towards the bottom of the discussion: LINK.

EDIT Jan 6th, 2012: Yuri added a suggestion about how to apply classes without writing any code by using the Context module. I think it makes for an excellent solution too. Link: http://drupal.org/node/1072806#comment-5124596

Comments

awasson’s picture

Following the discussion here: http://drupal.org/node/940484, I was able to render my taxonomy link by adding it to node.tpl.php with a line like this: print render($content['field_section']);

It's not even nearly what I'm looking for but it's something.

I'm more interested in preprocessing the page in template.php so that I can add the term as a string or a value in an array that I can sanitize and add as a body class.

Edit: A little further into it, I've discovered how to add to the body_classes array which now goes by the variable name $classes. Back in D6 I would have preprocessed my page but in D7 it seems that the correct route approach is to THEMNAME_preprocess_html() as follows:

function THEMNAME_preprocess_html(&$variables) {	
	$variables['classes_array'][] = 'my-new-body-class';
} 

The next step would be to grab my taxonomy terms and then add them to the classes_array but with the changes in D7, I'm having some trouble still.

I would have though that I could use something like the following in template.php (I've borrowed heavily from http://www.advomatic.com/blogs/amanda-luker/arm-yourself-drupal-bodyclasses)

function THEMNAME_preprocess_html(&$variables) {
	if (isset($variables['node']->terms)) {
	  // Add unique classes based on taxonomy terms
	  foreach($variables['node']->terms as $term_info) {
		$variables['classes_array'][] = 'terms-'.form_clean_id($term_info->name);
	  }
	}
}

This isn't working yet because I'm not using the right syntax to access the terms array and I can't seem to find it in the API docs yet. If anyone can point me in the right direction, I would appreciate it.

Thanks,
Andrew

awasson’s picture

After quite a bit of R&D and following various discussions on the subject of Taxonomy and how taxonomy_node_get_terms() isn't available in Drupal 7, I was directed to a thread with a workaround. It isn't perfect and it throws a notice in my admin pages but it will do and I'll track down the error.

Armed with this info, the solution to adding taxonomy terms to my body tag was quite simple.

  • I added the function [taxonomy_node_get_terms()] outlined in the aforementioned discussion to my template.php file
  • I called the function from within MYTHEME_preprocess_html() and looped through the terms to add them to $variables['classes_array'][] which automatically adds them to the body tag.

* I used drupal_clean_css_identifier() to sanitize the strings and then strtolower() to make them lowercase.

Here's the solution for now:


function taxonomy_node_get_terms($node, $key = 'tid') {
	static $terms;

	if (!isset($terms[$node->vid][$key])) {
		$query = db_select('taxonomy_index', 'r');
		$t_alias = $query->join('taxonomy_term_data', 't', 'r.tid = t.tid');
		$v_alias = $query->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
		$query->fields( $t_alias );
		$query->condition("r.nid", $node->nid);
		$result = $query->execute();
		$terms[$node->vid][$key] = array();
		foreach ($result as $term) {
			$terms[$node->vid][$key][$term->$key] = $term;
		}
	}
	return $terms[$node->vid][$key];
}

function MYTHEME_preprocess_html(&$variables) {
    $node = node_load(arg(1));
	// The vocabulary I'm after has a tid (term id) of '2'.  
	$results = taxonomy_node_get_terms($node,2);
	if(is_array($results)) {
		foreach ($results as $item) {
		   $variables['classes_array'][] = "taxonomy-".strtolower(drupal_clean_css_identifier($item->name));
		}
	}
	
}

NOTE: I am getting a Drupal notice of an unidentified property that is being declared in the taxonomy_node_get_terms() function and it indicates the problem is with this statement inside the loop$terms[$node->vid][$key][$term->$key] = $term;. I haven't resolved it yet but I thought I would point that out.

gurkcity’s picture

Hi, thanks for the code. I ran in the same "unidentified property" notice. My solution was to replace

$terms[$node->vid][$key][$term->$key] = $term;

withe this:

$terms[$node->vid][$key][$term-><strong>tid</strong>] = $term;

I am not sure, if it is the right coding way, but my body has the class listed and the notice is gone away. So it is a solution for me at the moment

awasson’s picture

Thanks for the patch but it didn't seem to make any difference on mine. I get the same error with [$term->$key] or with [$term->tid].

I only get those errors within the admin pages so I think I'll probably have to write a conditional to ignore the workings of MYTHEME_taxonomy_node_get_terms() whenever a user is accessing an admin page. I wonder if the error would go away if I wasn't using the D7 admin overlay... We'' when I come up with something I'll post back.

Thanks again for the patch and I'm glad it worked for your site!

Cheers,
Andrew

dartdev’s picture

gurkcity's solution seems to work for me, so it's working, thanks everyone.

upd: I was wrong, gives the following error:

Notice: Trying to get property of non-object in taxonomy_node_get_terms()

agoradesign’s picture

You need to check the args in the preprocess function, if a node is being loaded. I've included this before, so I didn't get the error. Just wrap the code inside the MYTHEME_preprocess_html in the following if-clause:

if(arg(0)=='node' && is_numeric(arg(1))) {

Then you should never get an error message. Alternatively you could check if the node is empty after loading it.

marblegravy’s picture

OK, with all the pointers on here, this version definitely worked for me with no errors -

<?php
/**
 * Add a css class to the body based on the taxonomy values applied to a page.
 */
function taxonomy_node_get_terms($node, $key = 'tid') {
    if(arg(0)=='node' && is_numeric(arg(1))) {  
        static $terms;
        if (!isset($terms[$node->vid][$key])) {
            $query = db_select('taxonomy_index', 'r');
            $t_alias = $query->join('taxonomy_term_data', 't', 'r.tid = t.tid');
            $v_alias = $query->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
            $query->fields( $t_alias );
            $query->condition("r.nid", $node->nid);
            $result = $query->execute();
            $terms[$node->vid][$key] = array();
            foreach ($result as $term) {
                $terms[$node->vid][$key][$term->$key] = $term;
            }
        }
        return $terms[$node->vid][$key];
    }
}

function theme_preprocess_html(&$variables) {
    $node = node_load(arg(1));
    $results = taxonomy_node_get_terms($node);
    if(is_array($results)) {
        foreach ($results as $item) {
           $variables['classes_array'][] = "taxonomy-".strtolower(drupal_clean_css_identifier($item->name)); 
        }
    }
}
?>
jeffnine’s picture

Do you have to modify anything on this besides changing "theme" in function theme_preprocess_html to the name of my theme? I've done that, and placed in template.php but can't get it to work. Wondering if I am missing something else.

marblegravy’s picture

As long as you've changed theme_preprocess_html to yourtheme_preprocess_html and put the lot in your template.php file it should be working.

The only other dependencies are that:

  • You have a taxonomy vocabulary set up
  • that vocabulary is being applied to your page
  • you have selected at least one term from that vocabulary

If it's not working for you still try adding a print('
GOAT

'); at the start of your preprocess_html function and make sure it's firing OK.

jeffnine’s picture

Thanks. Not sure what I had wrong, but the function wasn't firing. Made some changes and now it works. Must have had a typo I was missing before. Works great now.

erok415’s picture

Does anyone have any idea how to remove a class from the body?

I have tried the code below but I can't get it to work. It is modified from D6 code.

function framework_process_html(&$vars) {
    // page
    // Renders a new page template to the list of templates used if it exists
    if (isset($vars['node'])) {
        if ($vars['node']->type != "") {
            $vars['theme_hook_suggestions'][] = 'page__' . $vars['node']->type;
        }
        if ($vars['node']->type == 'page1column') {
            $vars['sidebar_second'] = '';
            $vars['sidebar_first'] = '';
            if (($pos = array_search('one-sidebar', $vars['classes_array'])) !== false) { unset($vars['classes_array'][$pos]); }
            if (($pos = array_search('two-sidebar', $vars['classes_array'])) !== false) { unset($vars['classes_array'][$pos]); }
            if (($pos = array_search('sidebar-second', $vars['classes_array'])) !== false) { unset($vars['classes_array'][$pos]); }
            if (($pos = array_search('sidebar-first', $vars['classes_array'])) !== false) { unset($vars['classes_array'][$pos]); }
            $vars['classes_array'][] = 'no-sidebars';
            $vars['classes_array'][] = 'and-another';
        }
    }
}

E.

Ain't it great....!

loopy1492’s picture

I was having a similar issue and I found an article somewhere regarding the preprocess functions and it said to refresh the cache if you're not seeing your changes. I went to the Configuration > Performance page and refreshed the cache. After that, the changes to the function started firing. You may have just been screwing around with it for so long that the cache eventually just refreshed itself.

mikefyfer’s picture

This works perfectly for me as well, all selected taxonomy terms getting applied as body classes and no admin errors. Thanks!

dadderley’s picture

Thanks
I had the exact same requirement. I did a search and found this thread.
I applied your code to my template and it works flawlessly.
Very cool.

agoradesign’s picture

Hi,
you're using the taxonomy_node_get_term() function in a wrong way. The second parameter isn't meant to be an ID of a particular term - it is the name of a property used as array key. So theoretically you could use any property name of the taxonomy_term_data database table. The only one that makes sense is 'tid', which is the default value anyway. So your call should be:
$results = taxonomy_node_get_terms($node);

So gurkcity's solution is just hardcoding the 'tid', which is unnecessary, when the function is used correctly. So I'm wondering why other people still have errors with that solution. For me it works :-)

As this function prints all used taxonomy terms of any vocabulary used by the node, you would have to add an extra query condition, if you only want to print terms of a particular vocabulary id

awasson’s picture

@agoradesign
I've been meaning to thank you for the clarification. That makes sense and I've changed it in the instances that I'm using it. Unfortunately I can't edit the post I put up earlier.

Cheers,
Andrew

irwin nerwin’s picture

Joy! The code posted by marblegravy on May 23, 2011 works for us. (Here is our plea posted Aug 23, 2011.)

Worked as advertised, first time. Well, actually it didn't work. Then after flushes were cached, it did work. Causal or coincidental? (We have no idea what we're doing!) Er.. Verification needed.

However, we really don't need all the terms of all the vocabularies as classes. We just need the one-required-term-per-page term from one specific vocabulary. (We have no idea what we're doing! Heeellp!) Er... Studying how one would modify the code to do that....

marblegravy’s picture

Hey Irwin - I don't have time to test it all out, but at a quick glance, I'd say getting results for a specific vocabulary would be as easy as changing the references to node->vid to the vocabulary ID of the vocabulary you want to test for.

So if your Vocabulary is 'Album' as per your other post, has the ID of 4:
$myvid = 4;

and then change all of the:
$node->vid
to
$myvid

Let me know if that works for you. ;)

irwin nerwin’s picture

Thanks for the suggestion, marblegravy, but that still returned all taxonomy terms. (?!?)

I only about 10% know what I'm doing, but your suggestion led me to <strike>make a wild guess</strike> study and experiment, and I did this:

In function taxonomy_node_get_terms, after the line

$query->condition("r.nid", $node->nid);

I added these two lines:

$myvid = 2;
$query->condition("t.vid", $myvid);

with 2 being the vocabulary id I needed, of course. To my great astonishment, it worked! Woot!

Since it's a one-term-per-node vocabulary, I don't need the foreach loop, but I hesitate to fiddle with it further, following the ancient and venerable programmer's axiom, if it works, quit coding. :)

marblegravy’s picture

Well done!! Wanna post up your full function for future reference for folks trying to return a single taxonomy value for a specific vocabulary?

irwin nerwin’s picture

Okay. This code, placed in a theme's template.php file, is the same as the code as posted by marblegravy except, modified to constrain the taxonomy values to a single vocabulary, id#2 in my case, by adding these two lines:
$myvid = 2;
$query->condition("t.vid", $myvid);

To change the vocabulary id#, change the value for $myvid.

/**
 * Add css classes to the body based on a particular vocabulary's taxonomy values applied to a page.
 */
function taxonomy_node_get_terms($node, $key = 'tid') {
    if(arg(0)=='node' && is_numeric(arg(1))) {
        static $terms;
        if (!isset($terms[$node->vid][$key])) {
            $query = db_select('taxonomy_index', 'r');
            $t_alias = $query->join('taxonomy_term_data', 't', 'r.tid = t.tid');
            $v_alias = $query->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
            $query->fields( $t_alias );
            $query->condition("r.nid", $node->nid);
            $myvid = 2;
            $query->condition("t.vid", $myvid);
            $result = $query->execute();
            $terms[$node->vid][$key] = array();
            foreach ($result as $term) {
                $terms[$node->vid][$key][$term->$key] = $term;
            }
        }
        return $terms[$node->vid][$key];
    }
}

function mindfulness_preprocess_html(&$variables) {
    $node = node_load(arg(1));
    $results = taxonomy_node_get_terms($node);
    if(is_array($results)) {
        foreach ($results as $item) {
           $variables['classes_array'][] = "taxonomy-".strtolower(drupal_clean_css_identifier($item->name)); 
        }
    }
}
Yuri’s picture

body class based on taxonomy term can easily be done with the context module. No coding at all:
condition: select the taxonomy term
action: theme html: action class

awasson’s picture

Thanks Yuri,
That is a great tip!

Andrew

Pascal.s’s picture

This is the best solution for designers like me who are not so good with coding. Worked for me after 15 min. Drupal 7 Adaptive theme
Thanks Yuri!

camilo.escobar’s picture

All responses to this case were just a lot of clutter to complicate your life. But your suggestion is wonderful. Thank you very much.

Anonymous’s picture

Using the context module was almost too easy.

I had my taxonomy term body classes in place within 10 mins.

Thanks again..

irwin nerwin’s picture

Much-belated (like, more than a year later) thanks, Yuri, for the tip to the very useful Context Module! Does "all this and more!"

Thanks also expressed in a longer revised post on another thread.

gavjof’s picture

Thanks for posting this. Its a far easier technique. Looks like I can use the Context module for a lot of other useful stuff too. ;)

jillpadams’s picture

This fix worked perfectly. I didn't love the long output for some of my term names, so switched it to say:
$variables['classes_array'][] = "taxonomy-".strtolower(drupal_clean_css_identifier($item->tid));
instead of name, so I could use ".taxonomy-1" in the css instead. Saved me a load of time - thanks!

dzaus’s picture

I found this to get the taxonomy terms for a node:

$terms = field_view_field('node', $node, 'field_tags');

where presumably field_tags is the name of your taxonomy field. Then instead of render($terms) to print your list, you'd probably have to do some digging through the renderable array.

Or (untested):
print render($content['field_tags']);

You can also use the following to give you a list of tids:
$tids = field_get_items('node', $node, 'field_tags');

soulston’s picture

This is how I accomplished it in D7 - less abstracted but if you just want simple terms.

Replace MY_THEME, NAME_OF_YOUR_FIELD and MY_NODE_TYPE with yours and remember to flush the cache as you are adding a new preprocess function in template.php.


function MY_THEME_preprocess_html(&$vars, $hook) {
  // Add the terms to the body classes for MY_NODE_TYPE'.
  if ($node = menu_get_object()) {
    if ($node->type == 'MY_NODE_TYPE') {
      
      // Return an array of taxonomy term ID's.
      $termids = field_get_items('node', $node, 'NAME_OF_YOUR_FIELD');
      
      // Load all the terms to get the name and vocab.
      foreach ($termids as $termid) {
        $terms[] = taxonomy_term_load($termid['tid']);
      }

      // Assign the taxonomy values.
      foreach ($terms as $term) {
        $class = strtolower(drupal_clean_css_identifier($term->name));
        $vocabulary = drupal_clean_css_identifier($term->vocabulary_machine_name);
        $vars['classes_array'][] = 'taxonomy-' . $vocabulary . '-' . $class;
      }
    }
  }
}

muddie’s picture

Thank you, thank you. A million times thank you!
I wasted a whole day trying to figure this out. So many posts with outdated or buggy code.
Yours was perfect.
Thanks.

ericquigley’s picture

Even though this is a way old post, I just wanted to thank you for it. Just brought a lot of time spent to a successful close.

retiredpro’s picture

This solution worked for me once I added an additional line to the preprocess_html right next to the original $variables['attributes_array']['class'][]. Maybe it has to do with the way omega subtheme works?

$variables['attributes_array']['class'][] = "taxonomy-".strtolower(drupal_clean_css_identifier($item->name));

EDIT: Whoops, meant to reply to marblegravy's post above. http://drupal.org/node/1072806#comment-4502324

Dennis
oh-soyummy.com

Jelmer85’s picture

Thank you so much! I have tried many different codes to do this and all failed, except the solution displayed in your start post :-)

bib_boy’s picture

hi, i can get this working for a body class, but how about a node class? Any ideas? I have a view of nodes and want to style each depending on their taxonomy term.

bib_boy’s picture

OK that was easy enough. I used theme_preprocess_node {} instead of theme_preprocess_html

BUT I also have a view of nodes on one page. How do I pass the taxonomy class into each of these nodes using this method?

agoradesign’s picture

Hi,
it depends, if your view display is field-based or node-based:

If you display nodes (no matter if full view mode or teaser view mode), you don't need to change anything because your theme_preprocess_node() function will also be called here.

If you are using a field based output of the view, it isn't tough either. Make sure, that you output your taxonomy as field. If you don't want it to be visible, just check the "exclude from display" checkbox on the field configuration. Next click on the configuration link of the view's format (e.g. HTML list). E.g. for the html list you can provide css classes for every li item. You can either provide static values there, or - and that's what you need - you can use replacement patterns here. You can find out the name of the corresponding replacement pattern for your taxonomy field, if you check the "Rewrite the output of this field" setting on any of your fields in the view.

antonyanimator’s picture

Thanks for the comment,
I have another question. I am currently loading a taxonomy vocabulary into a view, it contains two terms. A user can select only one term. I want each term to have a different class. (e.g. I want to theme each term one red and one green) The only way I can do this is to create two seperate displays and filter by the exact term. This seems to me to be an cumbersome way to do this.

Is there a way around this without using code?

Thanks

walangitan’s picture

Edit: Solution Below
I'm not able to get the preprocess_node to add the class while the node is in a view.
I'm using a view that's displaying view modes.
This works fine if I view the node type the view mode using a dpm($classes) on the node, but when the it's in a view the classes aren't being added to the node. Any ideas?

function MY_MODULE_preprocess_node(&$vars, $hook) {
  // Add the terms to the body classes for MY_NODE_TYPE'.

  if ($node = menu_get_object()) {
    if ($node->type == 'MY_NODE_TYPE' && $vars['view_mode'] == 'MY_VIEW_MODE') {

      // Return an array of taxonomy term ID's.
      $termids = field_get_items('node', $node, 'field_category');

      // Load all the terms to get the name and vocab.
      foreach ($termids as $termid) {
        $terms[] = taxonomy_term_load($termid['tid']);
      }

      // Assign the taxonomy values.
      foreach ($terms as $term) {
        $class = strtolower(drupal_clean_css_identifier($term->name));
        $vocabulary = drupal_clean_css_identifier($term->vocabulary_machine_name);
        $vars['classes_array'][] = $class;
      }
    }
  }
}

Edit: Solution
I was able to finally get it to call the classes properly. Appears that these conditions were not being met in the view:

if ($node = menu_get_object()) {
    if ($node->type == 'my_node_type') {

so I removed that line and instead replaced it with
$node = $vars['node'];
Removed the code from my custom module and placed it in my custom theme's template.php so the function now looks like this

function MY_THEME_preprocess_node(&$vars, $hook) {
  // Add the terms to the body classes for MY_NODE_TYPE'.
  $node = $vars['node'];

  // Return an array of taxonomy term ID's.
  $termids = field_get_items('node', $node, 'MY_FIELD_NAME');

  // Load all the terms to get the name and vocab.
  foreach ($termids as $termid) {
    $terms[] = taxonomy_term_load($termid['tid']);
  }

  // Assign the taxonomy values.
  foreach ($terms as $term) {
    $class = strtolower(drupal_clean_css_identifier($term->name));
    $vocabulary = drupal_clean_css_identifier($term->vocabulary_machine_name);
    $vars['classes_array'][] = $class;
 }
}

I still had to define $node as $vars['node'], otherwise I receive an error. The classes now generates a taxonomy term as a class in a view as well as a node.

Prancz_Adam’s picture

This is a litle bit off, but I think the solution is almost the same....

My question is how can I add rel="tag" microformat if i displayed the terms as a link on a selected (tags) vocabulary only?

Anonymous’s picture

Hi,

first of all thanks for this piece of code. It works perfectly!

My recent problem is getting this working in combination with an Omega3 Subtheme of mine. I have tried everything but just can't get this working.

I am not a crack in PHP so be patient with me. How do I address this with an Omega Subtheme? I guess it has something to do with the preprocess naming. My Subtheme is called "animares" so I tried this:

function animares_alpha_preprocess_html(&$variables) {
    $node = node_load(arg(1));
    $results = taxonomy_node_get_terms($node);
    if(is_array($results)) {
        foreach ($results as $item) {
           $variables['classes_array'][] = "taxonomy-".strtolower(drupal_clean_css_identifier($item->name));
        }
    }
}

But it doesn't work with "alpha" nore "omega"?????

Any Ideas???

Best regards
Jay

jaypan’s picture

You don't include the parent theme name for the hook, you only use your theme name. So you want:

function animares_preprocess_html(&$variables) {

Note that you should also change your code to be a little cleaner by doing this:

function animares_preprocess_html(&$variables) {
  $node = menu_get_object();
  if($node)
  {
    $results = taxonomy_node_get_terms($node);
    if($results) {
      foreach ($results as $item) {
        $variables['classes_array'][] = "taxonomy-".strtolower(drupal_clean_css_identifier($item->name));
      }
    }
  }
}

Contact me to contract me for D7 -> D10/11 migrations.

jalapi’s picture

Hi guys,

Could any one shed some light on how I could print the parent term of a term that is assigned to a node to the body classes_array? I've been searching through forums all day and can't seem to find the solution. I have been able get the assigned term to print to the body class using the following code:

Added this to template.php in bootstrap:

function taxonomy_node_get_terms($node, $key = 'tid') {
    static $terms;

    if (!isset($terms[$node->vid][$key])) {
        $query = db_select('taxonomy_index', 'r');
        $t_alias = $query->join('taxonomy_term_data', 't', 'r.tid = t.tid');
        $v_alias = $query->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
        $query->fields( $t_alias );
        $query->condition("r.nid", $node->nid);
        $myvid = 4;
		$query->condition("t.vid", $myvid);
        $result = $query->execute();
        $terms[$node->vid][$key] = array();
        foreach ($result as $term) {
            $terms[$node->vid][$key][$term->$key] = $term;
        }
    }
    return $terms[$node->vid][$key];
}

Added this to the html.vars in bootstrap:

function bootstrap_preprocess_html(&$variables) {
if(arg(0)=='node' && is_numeric(arg(1))) {
        $node = node_load(arg(1)); 
        $results = taxonomy_node_get_terms($node);
        if(is_array($results)) {
            foreach ($results as $item) {
               $variables['classes_array'][] = "taxonomy-".strtolower(drupal_clean_css_identifier($item->name));
            }
       }
   }
}

Any help would be truly appreciated!

Mackee’s picture

If you have the term id, there's a function called taxonomy_get_parents() see: https://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/funct...

knalstaaf’s picture

This worked well for me…

Pascal.s’s picture

This works great too! It's true it is easy with Context but i don't really like to install a module just to add this function. Plus this is easier!