By Fixdit on
[taxonomy] => Array
(
[27] => stdClass Object
(
[tid] => 27
[vid] => 3
[name] => handheld
[description] =>
[weight] => 0
)
[13] => stdClass Object
(
[tid] => 13
[vid] => 7
[name] => Technology
[description] =>
[weight] => 0
)
[9] => stdClass Object
(
[tid] => 9
[vid] => 2
[name] => Phone
[description] =>
[weight] => 0
)
[11] => stdClass Object
(
[tid] => 11
[vid] => 4
[name] => Nokia
[description] =>
[weight] => 0
)
[20] => stdClass Object
(
[tid] => 20
[vid] => 8
[name] => Current
[description] => Object is the up-to-date version.
[weight] => 0
)
)
I want to grab the 'Brand' or fourth name in each node that will contain these terms. What's the best way to loop through the taxonomy array to get to the fourth value each time?
<?php print $node->taxonomy[10]->name;?>
Will display 'Nokia', but I need the value of the brand everytime, not just term 11's name.
Cheers, hope I have explained this alright.
Comments
foreach ($node->taxonomy as
foreach ($node->taxonomy as $tid) {
print $tid->name;
}
Thanks for the reply joahim,
Thanks for the reply joahim, however I required just the fourth value.
Silly me, got
Silly me, got it...
Pro Drupal Development from London
ashleydavison [at] enjoystudios.co.uk
A word of caution.
There may be some conditions that might change the order of the taxonomy array in the node. I never trust an unordered array. That being said you could look for the vocabulary id to make sure you grab the right term for the "brand". For example:
Unordered array
Thanks Brian, I made that mistake. It's a good idea to use the vid. Cheers.
Sorry, late at night and
Sorry, late at night and didn't catch the bit about the fourth one... :)
Joachim, just wanted to thank
Joachim, just wanted to thank you for this bit of code. I was tearing what little hair I have left out of my head trying to display a node page that I wanted to embed the results of the taxonomy for that node. I couldn't just link to the results of the taxonomy as I wanted other node-specific info on the page as well. I had a View that grabbed all the correct taxonomy results, but I needed to pass that taxonomy ID to it.
For anyone else needing the ID of the taxonomy item and not the name to pass it in an embedded view for a node, here's what I used: