Finally. Taxonomy Based node.tpl.php

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(&amp;$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!

13 Comments

Great, glad I could help. It definitely took me some time to hunt this one down. While I don’t take credit for the snippet I thought it would be helpful to share and link out to the source in hopes that it might help someone find it a bit quicker. As always things change in open-source so it definitely doesn’t hurt to do some research/testing to be sure this is current.

Thanks for the comment!

Hey Jeremy, thanks for all your work. I was wondering what version of drupal you were using with this snippet?

This function is exactly what i’m looking for but i get drupal’s white screen of death when using this snippet in my template.php file. I tried it in both Garland and a custom built theme. the version of drupal i did the testing is 6.16.

any experience with this concept lately?

Thanks again.

Hey Casey – I can tell you for certain that is was pre Drupal 6 and about as long since I actually dug into a Drupal powered site.

Perhaps I will do some research on this and get back to you on it. In the mean time if you happen to find a new solution for how you accomplish this post Drupal 6, please feel free to drop by and leave it in the comments.

No worries, only spam if it includes Viagra links. Thanks for trying to post up the new code, you might have better luck trying to add it to http://pastie.org and then linking to it from the comments. If you would like to of course. I am not sure how much help I would be, Drupal is definitely not one of my strong points and obviously you know what you are doing, but I would be interested to check it out.

good idea. here is my complete node.tpl.php file: http://bit.ly/c1aUf1
The layout is based on a custom css grid design from my designer.

I would like to learn how to put this in template.php and eventually a module.

Coming in a bit late here, but found this via a google search, and it’s still very useful!

I’ve just tried Jeremy’s code snippet within Drupal 6.20 and it works. Maybe just make sure you have already specified a node.tpl.php as well as a node-term-tid.tpl.php. You need the former before you can use the latter.

Just what I was looking for, thanks for posting!

It always amazes me – the wide variety of things that you find you need to specialise a template for!

Thanks again,

Kevin.

This is great. Just what I needed.

The code snippet as displayed to the screen is a bit broken because of how the ampersand displays.

Good catch, it’s been like for a while so I appreciate the heads up. I did away with a syntax highlighter plugin over a year ago and some of the code got chopped in transition, never caught this one. Hope you don’t mind but I edited out your code because got chopped in posting the comment, I went ahead and updated the code in the post instead.

Thanks!

Leave a Comment