Working with Drupal again today, getting ready to push out “version 2″ of a particular site I have been working on this year and I ran into a bit of a snag. I found something that needed cleaning, I knew what needed to be done, just wasn’t sure how to do it. As anybody who has had the pleasure of working with Drupal you know that there is a ton of info out there than can teach you just about anything you need to know. Problem is, or at least what I have found to be a problem, is that it is not always extremely easy to find. So I’m sharing this snippet here because it took forever for me to dig up. Basically this snippet of code, when inserted into your themes template.php file, will allow you to create a node.tpl.php based on a taxonomy term.
Background Example
On a particular site that I am working with right now I was hoping to create a node template file that would allow me to style both the teaser and the main page in a slightly different manner than I had for all of the others site-wide. Nothing to exciting really it had to do with displaying quotes in a certain way. I knew that there had to be a way to implement this using template.php but being somewhat new at drupal I wasn’t exactly sure off the bat.
Needless to say, after about 3 hours of searching I finally found this snippet on drupal.org contributed by grendzy. “preprocess_node” of course that makes a lot of sense. Basically what this does is makes a node template such as node-term-14.tpl.php available for use, term 14 being the number of your taxonomy term.
<?php/** * Node-term.tpl function * Add this to your theme's template.php, replace "mytheme" with your theme's name */function mytheme_preprocess_node(&$vars) { foreach ($vars['node']->taxonomy as $term) { $vars['template_files'][] = 'node-term-'. $term->tid; }}?>Unfortunately there was not a whole lot of feedback on this particular thread so I can not say for sure that this will work for everyone, nor can I say for sure that it will be error free. I can say that it has worked perfectly for my situation and for that I am thankful.
Have a suggestion for a post or see something that we could improve, please let us know!
Super-helpful. Exactly what I needed. Thanks!