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

ravibarnwal’s picture

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

<?php if(term == 'a'){ ?>
<h1 style="color: red">
<?php }
else{ ?>
<h1 style="color: red">
<?php } ?>
kristin.e’s picture

Thanks @ravibarnwal - looks just like what I am looking for. I will test this out :)

kristin.e’s picture

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

<?php print render($title_prefix); ?>
      <?php if (!empty($title)): ?>
      

        <?php if(term == 'virtual'){ ?>
          <h1 class="page-header virtual"><?php print $title; ?></h1>
              <?php }
              elseif (term == 'cloud') { ?>
                <h1 class="page-header cloud"><?php print $title; ?></h1>
              <?php } 
              <?php }
              elseif (term == 'accounts') { ?>
                <h1 class="page-header accounts"><?php print $title; ?></h1>
              <?php }

              else{ ?>
              <h1 class="page-header"><?php print $title; ?></h1>
              <?php } ?>

      <?php endif; ?>
      <?php print render($title_suffix); ?>

I'm probably missing something obvious. Any pointers?

nevets’s picture

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

<?php
// Make sure we have no spaces in the term.
$class = str_replace(' ', '-', $term);
?>
<h1 class="page-header <?php print $class; ?>"><?php print $title; ?></h1>

This way you do not need the 'if' statements, and it handles new terms without new code.

kristin.e’s picture

Thanks @nevets. How do I set $term to mean taxonomy term? I'm not much of a coder. It would be great to know.

nevets’s picture

In that case I would really would suggest the Field Formatter CSS Class module

kristin.e’s picture

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']);

ravibarnwal’s picture

Yes but you have to find tid(term tid) from your node object.
you can find tid like this

<?php 
$tid = $node->your_field_name['und'][0]['tid']; 
$term = taxonomy_term_load($tid);
if($term->name == 'virtual'){
 
}
?>
kristin.e’s picture

Thanks so much @ravibarnwal for the very helpful reply - its all working now just like I need it to. :)))

Romon’s picture

Thanks for this reply.This reply will help me a lot

nevets’s picture

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.

kristin.e’s picture

@nevets I may check this out as an alternative if I can't get it to work in code. Thanks :)