1

So I have a website (created with drupal 7) where each product is linked to a taxonomyterms. This was done so that all products can easily be divided into several different categories.

So I have a taxonomy that looks somewhat like this:

Top level item 01

  • Level 2 item 01
  • Level 2 item 02
  • Level 2 item 03

Top level item 02

  • Level 2 item 04
  • Level 2 item 05
  • Level 2 item 06

Every 'top level' term (or categorie) has links that need to be in their own color. For Example: The product that is linked to the term 'Top level item 01' and all linked subitems need to have 'red' links. While the product that is linked to the term 'Top level item 02' and all linked subitems need to have 'blue' links.

I was thinking of adding a css class somewhere in my theme so that I could do something like:

.red a { color: #f00; }

But I can't seem to find how I should do that. Can aynone help me with that?

0

Try something like this in your theme's template.php file (put it into MY_THEME_preprocess_page(&$variables) function):

if(arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
    $tid = arg(2);
    $term = taxonomy_term_load($tid);
    $parent = taxonomy_get_parents($term->tid);

    if(empty($parent)) {
      $variables['classes_array']['parent_cat_color'] = $term->name;
      // or $variables['classes_array']['parent_cat_color'] = 'parent_cat_color' .. it's up to you..
    }
  }

and open page.tpl.php and put $classes variable into your wrapper div..

  • That doesn't seem to work, since I'm using the commerce module the taxonomy terms are linked to product pages and to show the product on an actual page they had linked to a 'display node' content type. So 'arg(0)' returns 'node' and arg(1) returns the node number, but not the taxonomy term. Also doesn't "taxonomy_get_parents" only get the very first parent of the item? since the top level item and the all of it's child items need the same class I guess I need to change it to 'taxonomy_get_parents_all' and then loop through the array it returns? – Nathan Dec 18 '13 at 9:19
  • @NathanH it doesn't matter you are using commerce or ubercart or whatever, taxonomy acts the same.. arg(0) == 'taxonomy' && arg(1) == 'term' works only when you are on a term page, arg(0) == 'node' && is_numeric(arg(1)) works when you are on node pages.. – xurshid29 Dec 18 '13 at 9:30
  • since arg(0) returns 'node' on my product pages, arg(1) will return the 'node id' and not the 'taxonomy id'. This means that 'taxonomy_term_load' returns an empty object because it can't find a 'taxonomy term' using a 'node id' (or at least that's how it works when I tried it). – Nathan Dec 18 '13 at 10:20
  • @NathanH it's ok.. For node pages you can access to terms by referenced field values. e.g. $node->field_REF_FIELD['und'][0]['tid']... – xurshid29 Dec 18 '13 at 10:25
0

Add the vid (=vocabulary id) to the classes in field.tpl.php. Then you will have unique classes for every vocabulary and their childitems.

the vid is available in the $items-array.

  • Maybe I misunderstood, but doesn't this mean that every item will get a unique class? Because I need the to have the 'top level' item to have the same class as all of it's childitems. – Nathan Dec 18 '13 at 9:24
  • vid is top level. it is a number. So you can style elements in that field (your tags) by doing .field_name.vid e.g. .field-taxonomy.1. – Marcel Dec 18 '13 at 16:26

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.