8 September 2011

How to get taxonomy term depth or level in Drupal 7.

Source code viewer
  1. $tid = 1; // your term id(tid)
  2. $limit = 99;
  3. $depth = 1;
  4. while($parent = db_query('SELECT parent FROM taxonomy_term_hierarchy WHERE tid = '.$tid)->fetchField())
  5. {
  6. $depth++;
  7. $tid = $parent;
  8. if($depth > $limit) break;
  9. }
Programming Language: PHP