6 April 2011

This tutorial shows how to get taxonomy terms names in Drupal 7. First how to get all taxonomy terms, how to get categories by a parent and with a little effort, you can turn it into a treeview.

Get All Taxonomy Terms Names:

Source code viewer
  1. $terms = db_query('SELECT name FROM taxonomy_term_data');
  2. foreach ($terms as $term) {
  3. echo $term->name;
  4. }
Programming Language: PHP

Get Taxonomy Terms Names Of Certain Parent:

This query gets all taxonomy terms names of certain parent. In this case it gets all the main terms. You can make a recursive function and make a treeview of categorie names. Tutorial that shows how to make recursive funtion: Tutorial: Create Treeview Using Recursion in PHP.
Source code viewer
  1. $terms = db_query('SELECT taxonomy_term_data.name AS name, taxonomy_term_data.tid AS tid FROM taxonomy_term_data, taxonomy_term_hierarchy WHERE taxonomy_term_data.tid = taxonomy_term_hierarchy.tid AND taxonomy_term_hierarchy.parent = 0');
  2. foreach ($terms as $term) {
  3. echo $term->name;
  4. }
Programming Language: PHP