Skip to content

Instantly share code, notes, and snippets.

@rachellawson
Created September 4, 2012 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rachellawson/3621319 to your computer and use it in GitHub Desktop.
Save rachellawson/3621319 to your computer and use it in GitHub Desktop.
Adding current taxonomy terms to body.classes
/*
* Lookup the taxonomy terms that are relevant to the supplied node
*/
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];
}
/**
* implements THEMENAME_alpha_preprocess_HOOK Omega function
* Looking to inject current terms into body classes to enable different styles per section
*/
function mundi_cy_alpha_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['attributes_array']['class'][] = "taxonomy-".strtolower(drupal_clean_css_identifier($item->name));
}
}
}
}
@rachellawson
Copy link
Author

Specifically note how to add the classes to an Omega sub-theme. It's a bit different to most themes...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment