On my site I have a node type that can only be assigned one category per node, and I would like each node to simply print the term that applies to that node. I know that I can print terms using:

  <?php if ($terms): ?>
    <span class="terms"><?php print $terms ?></span>
  <?php endif;?>

But rather than an unordered list with a single term that is a link to that category, I would like to have the node simply print out the raw term name into my html so that I can do whatever I want with it. This is probably a really simple piece of code but I can't find any reference to doing this anywhere. Thanks for any help.

Comments

WorldFallz’s picture

if there's only one term, then you should be able to use taxonomy_node_get_terms

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

jacobmn’s picture

Well, there are numerous terms within the category. But only one can be assigned to each node, so I want to simply print that one term out in the node. No html, just raw data, so I can plug it in wherever I want in the node.tpl.php file.

styro’s picture

If there is only one term attached to that node, the array taxonomy_node_get_terms() returns will only have one item containing the raw data for that one term.

It sounds pretty much exactly like what you want.

--
Anton

jacobmn’s picture

Ok, so after playing with this and searching and searching I have to come crawling back with my tail between my legs. I cannot figure out how to use the taxonomy_node_get_terms array in my node.tpl.php file. When I use:

<?php print taxonomy_node_get_terms($key = 'tid'); ?>

I just get the word "Array" output into the node. I am sure this is very newbish and I need to study up on my php and drupal API knowledge, but a little bump in the right direction - even just a link to some vague explanation - would be very helpful. Thanks again!

WorldFallz’s picture

you can use print_r to see the structure of the array you need to manipulate.

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

jacobmn’s picture

So when I use print_r to look at the structure of the taxonomy array, I get this:

[taxonomy] => Array
        (
            [6] => stdClass Object
                (
                    [tid] => 6
                    [vid] => 1
                    [name] => My Term Name
                    [description] => 
                    [weight] => -2
                )

        )

So based on that I would assume this would work:

<?php print $node->taxonomy['name'] ?>

But all I get then is no output at all in the node, but no errors. What am I doing wrong?

IbnKuldun’s picture

Check out this page.
http://drupal.org/node/53089

jacobmn’s picture

Great, thanks much for that link! Based on some of their modified code I have come up with this method for displaying simply the term name with no links, no frils. I have mine printed out in a span, but obviously this could be modified to fit within any element.

<?php if ($terms): ?>
<?php
  $vocabularies = taxonomy_get_vocabularies();
  foreach($vocabularies as $vocabulary) {
	if ($vocabularies) {
	  $terms = taxonomy_node_get_terms_by_vocabulary($node->nid, $vocabulary->vid);
	  if ($terms) {
	  print '<span class="term">';
		foreach ($terms as $term) {
		  print $term->name;
		}
	  print '</span>';
	  }
	}
  }
?>
<?php endif; ?>
summit’s picture

Subscribing,

Do you know how to get this as an option for a cck-field,to get the term-description printed within a cck-node as a field?
Thanks a lot in advance for your answer!

Greetings,
Martijn

TrinitySEM’s picture

For CCK try:

print content_format('field_cck_field_name', $field_cck_field_name[0]);

Retain "field_" and replace "field_name" with the actual name of your CCK field.

Jboo’s picture

Does anyone know how to use this in D6?

Thanks for any help.

My new EasySnoozing, Nursing and BusinessEgghead websites on D7!

WorldFallz’s picture

The only obvious thing that's changed for d6 that i see is the taxonomy_node_get_terms_by_vocabulary function. Change to taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid)

Jboo’s picture

Thanks very much, that's what the problem was!

My new EasySnoozing, Nursing and BusinessEgghead websites on D7!

halloffame’s picture

How do you print this with first/last li class?

netsensei’s picture

... I did it like this:

<THEMENAME>_preprocess_node(&$vars) {
    $data = array();
    foreach ($vars['taxonomy'] as $term) {
      $data[] = $term['title'];
    }

    $vars['terms']  = theme('item_list', $data, NULL, 'ul', array('class' => 'items inline'));
}

Explanation:
First, I don't like the solutions where taxonomy functions are called. Especially if you are going to use them in preprocess functions. Everything you need is already stored in $variables. Why call a function which fetches it again from the database?

Anyway, the idea is to use theme_item_list() instead of theme_links to make an unlinked list. Overriding the node preprocess function is the easiest way to achieve this. The thing is that both functions want the taxonomy data passed in different formats. That's why I need to take $vars['taxonomy'] - which has the taxonomy data - and put it through a foreach before I can pass it to the theming function.

Everything else (floating, in line, backgrounds,...) is - of course - just plain old CSS trickery.

priceline’s picture

_preprocess_node(&$vars) {
$data = array();
foreach ($vars['taxonomy'] as $term) {
$data[] = $term['title'];
}

$vars['terms'] = theme('item_list', $data, NULL, 'ul', array('class' => 'items inline'));
}

This works great. If i have two vocabularies, i should separate them; currently it is showing all the terms regardless of vocabulary.

the_therapist’s picture

I had a content type with one category assigned to each node. I wanted the title to be [category]: [title] so I simply did this:

<?php print trim(strip_tags($terms)).": ".$title ?>
winawer’s picture

Hello. i want to print taxonamy term in a node and see this link (#224171) useful.

This code work for me:

<?php
foreach ( (array)$node->taxonomy as $term ) {
echo $term->name;
}
?>

I hope this help.
Bye :)

warmth’s picture

I would suggest Display Suite module or some jQuery

jQuery(".taxonomy-term h2 a").each(function(){
var $t = jQuery(this);
$t.after($t.text());
$t.remove();