Render term hierarchy in one line, separated by hyphen

Public

Below sample code will render term hierarchy in one line. For e.g.
* Home
* * Furniture
* * * Table
* * * * Dinner Table

will be rendered as below:
"Home-Furniture-Table-Dinner Table"

</> CopyGet raw version
php
  1. $tid = 10; // Child term ID
  2. if (!empty($tid)) {
  3. // Load all parents of child term.
  4. $terms = taxonomy_get_parents_all($tid);
  5. // Loop through all parents and create hierarchy
  6. foreach ($terms as $key => $term) {
  7. $hierarchy[$key] = $term->name;
  8. }
  9. // Sort parent terms.
  10. krsort($hierarchy);
  11. }
  12.  
  13. // Implode all parents using hypen (-).
  14. $term_hierarchy = implode('-', $hierarchy);
  15. echo $term_hierarchy;