I'm trying to pull the first term from a nodes terms, however I'm having trouble getting around the need to know the Terms TID. Using the following code I get the result, but the number represents the TID, which is going to fall over the moment I dont know what the term is.

print $node->taxonomy[1]->name;

I'm going to use the term as an argument for a view i've created and need to create the link for this using the term name.

Any help would be appreciated.

Comments

nevets’s picture

How about

if  ( is_array($node->taxonomy) ) {
  $keys  =  array_keys($node->taxonomy);
  $first_key = array_shift($keys);
  print $node->taxonomy[$first_key];
}
andrew_hoffi’s picture

I've played around with the code, but it doesn't seem to work for me. I think the problem I have is referencing the array with a number and not "taxonomy_term_1" here's what I have to play with:

Array
(
    [taxonomy_term_1] => Array
        (
            [title] => FirstTerm
            [href] => taxonomy/term/1
            [attributes] => Array
                (
                    [rel] => tag
                    [title] => 
                )

        )

)
Array
(
    [taxonomy_term_2] => Array
        (
            [title] => SecondTerm
            [href] => taxonomy/term/2
            [attributes] => Array
                (
                    [rel] => tag
                    [title] => 
                )

        )

)

Thanks
Andrew
-------------------
www.hoffi.com

nevets’s picture

That looks like $taxonomy, not $node->taxonomy. Which are you using?

andrew_hoffi’s picture

Appologies you are correct I should be using $node->taxonomy. I managed to solve the problem following the code suggestion you gave.

	$tempTaxonomy = array_values($node->taxonomy);
	print $tempTaxonomy[0]->name;

Thanks
Andrew
-------------------
www.hoffi.com

k3vin’s picture

I got error with this line:

  print $node->taxonomy[$first_key];

This line is working for me:

  print $node->taxonomy[$first_key]->name;