2

I am having trouble getting my taxonomy terms printed into the containing of my node.tpl.php file. I have named my taxonomy terms for a custom block (books) as field_tags and I want them to get printed after the $classes array as plain text CSS classes.

Here is the offending line:

<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?><?php print $content['field_tags']; ?> clearfix"<?php print $attributes; ?>>

The goal here is to have a menu on top of this view of books that can select individual titles based on their individual taxonomy terms. Open to suggestions about that as well if you think this plan is stupid.

4
  • 1
    Which version of drupal are you using ? – Anil Sagar Oct 4 '12 at 6:17
  • Drupal 7. I have seen many answers to this question for Drupal 6, but it seems harder to find an answer for D7. – Jonathan Kempf Oct 4 '12 at 17:19
  • So you are trying to print node.tpl.php variables in a custom block ? – AKS Oct 4 '12 at 17:27
  • No. What I am trying to do is print the taxonomy terms of a specific node type into the wrapper div of that node. The custom node is node--books.tpl.php, and it gets printed into a block that is being controlled by views. So the end result is a page that has the 'books' content type being printed onto a page as a series of <li>'s, but I need each of those <li>'s to have their taxonomy as the class. – Jonathan Kempf Oct 4 '12 at 17:44
0

I think the proper way to do this would be to implement THEME_preprocess_HOOK() in your theme's template.php.

In that function, just add each taxonomy term name into the classes array. You'll probably also want to cleanse the term names as you put them into the classes array, dig around Pathauto or Transliteration modules to find a function to do the dirty work.

function mytheme_preprocess_node(&$vars) {
    foreach ($vars['node']->field_my_taxonomy_field[LANGUAGE_NONE] as $term) {
        $vars['classes_array'][] = pathauto_cleanstring($term->name);
    }
}
3
  • Interesting. I assumed it was going to be something with preprocess hooks, but I have not used them yet. Would this solution allow me to give the individual nodes their own associated taxonomy names and not just a list of all of them? – Jonathan Kempf Oct 5 '12 at 11:55
  • I would accept your offer as well ^^ I do my best with code, but I still need a lot of practice. – Jonathan Kempf Oct 6 '12 at 0:54
  • I've updated the answer with example code. You'll need to name the function properly for your theme, and adjust the field name in the second line to the proper machine name for the field you're using. Make sure pathauto module is installed and clear cache to get Drupal to pick up the new theme implementation. Also, I'm thinking it may be better to use the term's tid instead of the name, because the name could possibly change in time. Instead of the pathauto_cleanstring()... use "term-{$term->tid}" and you'll end up with a class like "term-76". – Kerasai Oct 10 '12 at 1:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.