1

I sucessfully printed the whole taxonomy tree using this code.

$voc = taxonomy_vocabulary_machine_name_load('product_sub_categories');
$tree = taxonomy_get_tree($voc->vid, 0, NULL, TRUE);
foreach ($tree as $key => $term) {
  print $term->name."</br>";
}

Now I want to separately print parent and child in order to custom theme them, because I already have a mark-up in which parent has separate <p class="parent_class"> Parent terms</p> and children has separate <ul class="child_class">Child terms</ul>.

How should I do this?


I have done it using taxonomy_get_children(), but it is giving me the children of parent of the taxonomy whose ID is 1.

  • child1
  • child2
  • child3

This is what I need, but now I want to get the children of parent child1. I tried to get them by reusing the above function but it give me this.

  • child1
    • children of child1
  • child1
  • child2
  • child3
2

Use nested foreach like below.

$terms = taxonomy_get_children(1);
foreach ($terms as $term) {
  $items[] = $term->name;
  $child_term = taxonomy_get_children($term->tid);
  if (!empty($child_term)) {
    foreach ($child_term as $child) {
      $items[] = $child->name;
    }

  }
}
return theme('item_list', array('items' => $items));
  • thanku @ganesan for your reply, but it gave me error for line $items[] = $term->name; – nomeer Mar 9 '15 at 8:53
  • @nomeer I think you have used $items variable as object somewhere for some other purpose. Please check that and you could rename the variable name. – Ganesan Mar 9 '15 at 9:12

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.