Skip to main content
1 of 2
Rick
  • 11
  • 3

Using drupal_add_css to dynamically add inline CSS based on term reference

I'm trying to create a CSS class called .bg-unit-color which will change depending on the taxonomy reference chosen

I've set up a course category taxonomy and then set up a color field within it. I've got to the point where I can render the color in my node--example.tpl.php file with the following:

<?php $term=taxonomy_term_load($node->field_example_category['und'][0]['tid']); $category_color=field_view_field('taxonomy_term',$term,'field_example_category_color'); echo render($category_color); ?>

I've also used the drupal_add_css function in my template.php file, so this works:

$new_color='black'; drupal_add_css(".bg-unit-color {background-color: $new_color;}", 'inline');

However I can't marry the two together to create what I'd like to do which is this:

drupal_add_css(".bg-unit-color {background-color: $category_color;}", 'inline');

Any help would be massively appreciated. Thanks in advance

Rick
  • 11
  • 3