I'm posting this since this might be usefull when you are getting lost retrieving data from a $node. Since I did not want to solve this using a unnecessary db_query or while loop, I played around retrieving information from the $node array, which is getting pretty complicated with a bunch of added fields for CCK. Also, the enclosed $taxonomy array is available through this. Since this information is so nearby anyway, I think it is a waste to use another sql query to retrieve this information.
From within my $node, the taxonomy array looks like this:
[taxonomy] => Array
(
[6] => stdClass Object
(
[tid] => 6
[vid] => 2
[name] => Uitpakparty
[description] =>
[weight] => 0
)
)
Off course for every node the [6] key won't be the same. To retrieve the term_name (category name) from a node you have to find out first which key is used in this array.
So I load the taxonomy array on it's own in a variable, and then retrieve the key as $tid (off course you can use this var from now on throughout your theme file):
$tax = $node->taxonomy;
$keys = array_keys($tax);
$tid = $keys[0];
$cat = $node->taxonomy[$tid]->name;
print 'category is '
print $cat;
Voila, the $tid is read out as a key, indexed as a $tid. Now I can get any value from the taxonomy array; this example will print out 'category is Uitpakparty.
Comments
Nice trick :)
I find arrays in PHP to be horribly confusing. I'll have to remember this one. I've asked the same question you did many times and no one has ever been able to answer it for me.
How about making a handbook page?
Michelle
--------------------------------------
See my Drupal articles and tutorials or come check out life in the Coulee Region.
Files
This also works for (attached) files at a node:
I changed the code a bit and made it smaller. Works like a charm :).
Thank you for this... I've
Thank you for this... I've been banging my head against the wall for the past 2 hours trying to figure out how to extract the taxonomy array values based on their location in the array.
This did the trick and assuredly saved me countless more hours of head banging. :)
Cheers!
Thanks for the cool trick.
That really solved it for me too.