0

Can anyone of you brainy folk suggest the best way of retrieving the taxonomy term on a node and both of it's parents then storing as variables on a php snippet? For example: Country->City->District output to $country, $city and $district?

Thanks in advance for all your answers!...

nb, this is what I get from print_r, [0] being the country, [1] being the area and [2] the district:

field_location_term] => Array
    (
        [und] => Array
            (
                [0] => Array
                    (
                        [tid] => 26
                        [taxonomy_term] => stdClass Object
                            (
                                [tid] => 26
                                [vid] => 5
                                [name] => B----a
                                [description] => 
                                [format] => filtered_html
                                [weight] => 3
                                [vocabulary_machine_name] => geographical_location
                                [metatags] => Array
                                    (
                                    )

                            )

                    )

                [1] => Array
                    (
                        [tid] => 27
                        [taxonomy_term] => stdClass Object
                            (
                                [tid] => 27
                                [vid] => 5
                                [name] => B----o
                                [description] => 
                                [format] => filtered_html
                                [weight] => 0
                                [vocabulary_machine_name] => geographical_location
                                [metatags] => Array
                                    (
                                    )

                            )

                    )

                [2] => Array
                    (
                        [tid] => 28
                        [taxonomy_term] => stdClass Object
                            (
                                [tid] => 28
                                [vid] => 5
                                [name] => M----w
                                [description] => 
                                [format] => filtered_html
                                [weight] => 0
                                [vocabulary_machine_name] => geographical_location
                                [metatags] => Array
                                    (
                                    )

                            )

                    )

            )

I thought the best thing to do would be

$country = $node->field_location_term['und'][0]['taxonomy_term']['name'];

But I get no output.

2
  • Check this question: Get all term from tax vocab Commented Jul 6, 2012 at 19:58
  • Isn't it always the way; I've been researching this one solidly for the past 3 days, just after I post the reply I find out my way works but I was entering it incorrectly, it should have been: $country = $node->field_location_term['und'][0]['taxonomy_term']->name; - DOH!
    – Ballboy
    Commented Jul 6, 2012 at 21:18

1 Answer 1

1

It appears from your printout that taxonomy_term is an object, not an array. So your code would look like this:

$country = $node->field_location_term['und'][0]['taxonomy_term']->name;

You might not be able to do the object->['array']->object->['key'] thing. It might need to be broken into:

$foo = $node->field_location_term['und'][0]['taxonomy_term'];
$country = $foo->name;
1
  • Thanks Beth, i think i must have been replying to myself when you answered the post - looks like great minds think alike : )
    – Ballboy
    Commented Jul 7, 2012 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.