25

I have a taxonomy called services and want to list out all the terms. I can load the vocab with

 $vocabulary = taxonomy_vocabulary_machine_name_load('Services')

but cannot seen to get all the terms out.

  • can you write detailed code? – monymirza Mar 4 '13 at 11:51
43

There are a few ways but I prefer entity_load():

$vocabulary = taxonomy_vocabulary_machine_name_load('Services');
$terms = entity_load('taxonomy_term', FALSE, array('vid' => $vocabulary->vid));

You could also use:

  • Can you please explain the advantages of using entity_load ? – niksmac Mar 4 '13 at 12:51
  • 3
    @NikhilM It's less characters to type than taxonomy_term_load_multiple() which is why I personally prefer it. Using taxonomy_get_tree() introduces unnecessary levels of complexity to the queries involved, unless you specifically need the terms in their original hierarchy (which OP didn't specify) – Clive Mar 4 '13 at 12:59
  • I like it, I think the answer below is a bit easier to read though. – LeBlaireau Mar 4 '13 at 13:07
  • @welovedesign Really? I prefer 2 lines over 6 but that's just personal preference :) As long as it's under 80 characters you're still adhering to Drupal's coding standards too – Clive Mar 4 '13 at 13:08
  • My +1 for the adhering to Drupal's coding standards – niksmac Mar 4 '13 at 13:23
18

I would like to use this code.

$name = 'YOUR_VOCAB_NAME';
$myvoc = taxonomy_vocabulary_machine_name_load($name);
$tree = taxonomy_get_tree($myvoc->vid);
foreach ($tree as $term) {
 echo $term->tid;
}

taxonomy_vocabulary_machine_name_load

taxonomy_get_tree

  • If you have a multilingual site, this does not work as desired. If I want all the terms, regardless of the language, this does not give you proper results. "taxonomy_get_tree" will parse results based on language. the above answer works for me – Vaibhav Jain Feb 25 '15 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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