phpdrupaldrupal-7drupal-taxonomy

CSS class assigned to a taxonomy term


Imagine a blog with 4 different taxonomy terms.
I want to display "all blog items," but every blog item with the term "design" will have a different look. So what I'd want is to have a CSS class for rows within a node. Am I saying that right?

I probably did this in a gross, really hacky way, but here it is:

if (isset($fields["tid"]->content)) {
  unset($classes);
  unset($class);
  unset($classarray);

  $classarray = explode(",", $fields["tid"]->content);
  echo '<div class="';

  foreach ($classarray as $class) {
    $class = str_replace('<span class="field-content">', "", $class);
    $class = str_replace("</span>", "", $class);
    $class = str_replace(" ", "", $class);
    echo strtolower($class) . " ";
    unset($class);
  }

  echo '">';
}
else {
    echo "<div>";
}

Then I had a </div> tag at the end of the file.

This does require you to add the taxonomy details as content when you define your content type, and hide it with some CSS.

Well, I hope this might help someone, even though it's bad form.


Solution

  • To display "all blog items," you'll need to create a view.

    You can then create a custom template for that view, altering it to insert the taxonomy term as a class name into the HTML.