Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By kristin.e on
Hi, I have created a content type which has a list of taxonomy terms associated with it. When adding a node of this content type I have a field where its term ref is selected. I'd like the title and sub-title of a page of this content type have particular colour according to its taxonomy term. What is the best approach for this?
Thanks in advance
Comments
using node templete
You should change on node.tpl.php . If you have colors name in term then you can do using in line css on title like
<h1 style="color: <?php print "your term";>">
or if you have different name from color
you should define class according to your term like
Thanks @ravibarnwal - looks
Thanks @ravibarnwal - looks just like what I am looking for. I will test this out :)
Hey there I tried this out
Hey there I tried this out and the title isn't rendering at all. Here is the code I'm using in the tpl.php
I'm probably missing something obvious. Any pointers?
You have term when you want
You have
term
when you want$term
, this assumes you have set $term somewhere.Note you can simplify the code for printing the title to
This way you do not need the 'if' statements, and it handles new terms without new code.
Thanks @nevets. How do I set
Thanks @nevets. How do I set $term to mean taxonomy term? I'm not much of a coder. It would be great to know.
In that case I would really
In that case I would really would suggest the Field Formatter CSS Class module
Ok but I do have some coding
Ok but I do have some coding skills and would prefer to do this within the tpl.php file and improve my coding skills rather than downloading another module. Would it be something like
$term = taxonomy_term_load(['tid']);
Yes but you have to find tid from your node object
Yes but you have to find tid(term tid) from your node object.
you can find tid like this
Thanks so much @ravibarnwal
Thanks so much @ravibarnwal for the very helpful reply - its all working now just like I need it to. :)))
Thanks # ravibarnwal
Thanks for this reply.This reply will help me a lot
An easier way is to use the
An easier way is to use the Field Formatter CSS Class which can be used to apply a css class to the node wrapper based on a taxonomy term.
@nevets I may check this out
@nevets I may check this out as an alternative if I can't get it to work in code. Thanks :)