Hello !

Here's my challenge (not to hard I think) :

I'm working on a new version of my website with Drupal. In this site, you can found two majors things : screencasts about how to use your softwares on a Mac, and software-reviews screencasts. In the categories, I created a "Screencast" category, and a "Software" category (or whatever in French...). In those categories, there different terms (one for each level of difficulty, and one for each software), but that's not the point...

I created CSS classes for screencast-type nodes and for softwares-type nodes, e.g. :

.screencast {
  padding: .5em;
  background-color: #66CCFF;
  border: solid 1px #99FFFF;
}

.soft {
  padding: .5em;
  background-color: #CCFFFF;
  border: solid 1px #99FFFF;
}

The normal background for a node is :

.node {
  margin: .5em 0 2em 0;
  background-color: #FFFFCC;
  border: solid 1px #FFFF99; 
}

What I want is to display the different color each time I specify : this node is about screencasts in the taxonomy.

I tried this in the node.tpl.php :

foreach ($node->taxonomy as $tid => $term_obj) if ($tid = 4) { print " screencast"; }

But it displays the alternate color on all the nodes with taxonomy, and as I don't know so much about the core of Drupal, I don't know how to call the different terms to display a different color (coded in the CSS, but CSS is not the problem here) given the category (e.g. : yellow if it's a normal article, red if it's important, blue if it's a screencast, green if it's a soft).

If someone has an answer...

Comments

coreyp_1’s picture

You need another "=".

foreach ($node->taxonomy as $tid => $term_obj) if ($tid == 4) { print " screencast"; }

- Corey

MacStyle.fr’s picture

Damn it !!

I've just forgotten this *** "=" !! Thanks a lot coreyp_1 ^-^ (it's where you can see my and the PHP, we are not friends xD).

--

http://www.macstyle.fr

FPJ’s picture

When I use this nice code, I get the follow message:

warning: Invalid argument supplied for foreach() in /home/dutch/domains/dutchspiders.nl/public_html/sites/all/themes/dutchspiders/page.tpl.php on line 35.

Can some one help me?

alex’s picture

What if you check if it's an array first:

if (is_array($node->taxonomy) ) { foreach ($node->taxonomy as $tid => $term_obj) if ($tid == 4) { print " screencast"; } }

HillsWeb.com

FPJ’s picture

First thank you for your help and time!

When i use your code:

<div class="section" id="<?php if (is_array($node->taxonomy) ) { foreach ($node->taxonomy as $tid => $term_obj) if ($tid == 1) { print " screencast"; } } ?>">\

Nothing is happing, no error message and no id change

alex’s picture

Either $node->taxonomy is not an array or $tid not equal 1. You can var_dump($node->taxonomy) to see what's in there

HillsWeb.com

FPJ’s picture

Sorry but I don’t have the knowledge to fix this problem at my own I think. I’m a designer, I only know xhtml and css.

When I visited a page with no term I get the follow code:

<div id="NULL" class="section"/>

When i visited a page with the term Eeual to 1, i get the follow code:

<div id="array(1) { [1]=> object(stdClass)#14 (5) { [" class="section" ]="" tid=""> string(1) "1" ["vid"]=> string(1) "1" ["name"]=> string(10) "Informatie" ["description"]=> string(0) "" ["weight"]=> string(1) "0" } } "> </div>

I only need to change my background image, based on a taxonomy term . That’s all!

alex’s picture

<div class="section" id="<?php if (is_array($node->taxonomy) ) { foreach ($node->taxonomy as $item) if ($item->tid == 1) { print " screencast"; } } ?> /">

Although I can't test it right now, sorry

HillsWeb.com

FPJ’s picture

Your great!:D It works, thank you so much !

One single question:When I want to use more terms, how can I do that? And a default image how can I do that?

Will this work:

<div class="section" id="<?php if (is_array($node->taxonomy) ) { foreach ($node->taxonomy as $item) if ($item->tid == 1) { print " screencast"; } ($item->tid == 2) { print " screencast2"; } ifelse { print " default"; } ?> /">

alex’s picture

I myself would put styles in an array, but as for your code the following should work:

if (is_array( $node->taxonomy ) ) { 
	foreach ( $node->taxonomy as $item ) {
		if ($item->tid == 1) {
			print " screencast"; 
		} else if ( $item->tid == 2 ) {
			print " screencast2"; 
		} else { 
			print " default"; 
		}
	}
}

HillsWeb.com

br4t’s picture

How can i apply this knowledge i just gained (thanks for that) to my left-sidebar?

I basically want to style the background of my sidebar-left based on the term that is used for the node?

Thx in advance.