Hello all,

I am creating a minimal theme for a small site. I am fairly new to drupal but can just about find my way around PHPTemplate.

One feature of the theme I would like to have is to allow the author to choose one of 4 background images which make up most of the content area background. When a user creates a new node they should be presented with the option of choosing a background image for the node. The background image is output as html in page.tpl.php.

I figured the best way of doing this was to create a taxonomy term for the background. I have created a vocabulary and added 4 terms with the values as the name of the jpg images; about-bg.jpg, contact-bg.jpg, etc. I am having issues getting the taxonomy data into page.tpl.php so I can extract the value and output it to select the jpg background image.

I've had a look and tried out taxonomy_node_get_terms but I cant seem to get anything out of it.

How can I get the taxonomy data available to use in page.tpl.php?
Thanks.

Comments

cwgordon7’s picture

Try the following code:

// Check to see if we're on a node/%node page.
if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
  // Load the node.
  $node = node_load(arg(1));
  $taxonomy_terms = $node->taxonomy[$vid]; // $vid should be the vocabulary id of your vocabulary.
}

$taxonomy_terms will then be an array containing the taxonomy terms on the current node.

matalo’s picture

Couldn't get this code to work but I managed to modify it. I found that $node->taxonomy[] was keyed by the termid, not the vocabularyid. Instead I just ran through all the terms looking for the vocabularyid I needed.

	if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
		$node = node_load(arg(1));
		$taxonomy_terms = $node->taxonomy;
		foreach ($taxonomy_terms as $term) {
			if ($term->vid == 1) {
				$ld_bg = $term->name;
			}
		}
	}

Thanks for your help

matt v.’s picture

The code above worked for me within page.tpl.php, but I'm curious if it's possible to do this in a preprocess function instead. I tried essentially the code above inside a theme_preprocess_page function, but I can't seem to get it working. Any suggestions?

matt v.’s picture

I found a thread that had a solution.

mattcasey’s picture

Thanks this worked for me!

mikebrooks’s picture

Just wanted to add that this works great in a custom tpl used to output a PDF (using the Print module).

My tpl file is print_pdf.node-project.tpl.php, where "project" is my content type.

I added a print clause in the foreach loop.

    if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
        $node = node_load(arg(1));
        $taxonomy_terms = $node->taxonomy;
        foreach ($taxonomy_terms as $term) {
            if ($term->vid == 3) {
                $ld_bg = $term->name;
                print $ld_bg;
            }
        }
    }

- Mike

d3zign7reak’s picture

Thanks so much! This thread helped me solve an issue that I was having where I only wanted to show "Share This" button on one page. But one question that I have is, where can I go to learn more about arg() in Drupal?

Thanks,

Alvin C.

matt v.’s picture