Community Documentation

Taxonomy Tree with feed links

Last updated April 12, 2007. Created by EnekoAlonso on March 8, 2006.
Edited by add1sun, pwolanin, Bèr Kessels. Log in to edit this page.

PLEASE NOTE: The php snippets are user submitted and it is impossible to check every one, so use at your own risk. When using an alternate database to the default MySQL, some database queries may not work.

This little code outputs the full taxonomy tree, including the number of nodes and feed link for each term.

<?php
 
function get_child_terms($parent, $vid) {
     
$sql = "SELECT td.tid, td.vid, td.name"
        
. "  FROM {term_data} td"
        
. "  JOIN {term_hierarchy} th on th.tid = td.tid"
        
. " WHERE th.parent = %d"
        
. "   AND td.vid = %d"
        
. " ORDER BY td.weight, td.name";
   
$terms = db_query($sql, $parent, $vid);
   
$output = "";
    while (
$aterm = db_fetch_object($terms)) {
       
$output .= "<li>";
       
$output .= theme('feed_icon', url("taxonomy/term/$aterm->tid/all/feed"))
                . 
" "
               
l("$aterm->name", "taxonomy/term/$aterm->tid/all") . " ("
               
taxonomy_term_count_nodes($aterm->tid) . ")</li>\n"
               
get_child_terms($aterm->tid, $vid);
    }
    return (
$output != "") ? "<ul>\n". $output ."</ul>\n" : "";
  }

 
$sql = "SELECT vid, name FROM {vocabulary} ORDER BY name";
 
$vocabularies = db_query($sql);
 
$output = "";
  while (
$avoc = db_fetch_object($vocabularies)) {
     
$output .= "<li><strong>$avoc->name</strong></li>\n"
             
get_child_terms(0, $avoc->vid);
  }
  print
"<div class='taxonomy_tree'><p><ul>\n". $output ."</ul></p></div>\n";
?>

With a little CSS style like this we can avoid showing list bullets, and also customize list margins:

.taxonomy_tree ul {
  list-style-type: none;
  margin: 0px 0px 0px 20px;
  padding: 0px;
}

I haven't find any module or code to do this simple thing, so I wrote it and I hope you guys like it.
You can see it working at http://spaniards.es/indice (the url alias requires path module).

Comments

Block php code to display taxonomy terms

The code was tested with Drupal 5.12.
I needed a block that would display all terms from chosen vocabulary.
Views were working until several nodes had assigned the same taxonomy term - then views started displaying the same term several times. I did some research and come up with this code (created a block with php input and added the code there)

<?php
$vid
= 1; // replace 1 with your vocabulary id
$vocabulary = taxonomy_get_vocabulary($vid);
$tree = taxonomy_get_tree($vocabulary->vid);
print
'<ul>';
foreach (
$tree as $term) {
  print
'<li>'. l($term->name, "taxonomy/term/$term->tid") .'</li>';
}
print
'</ul>';
?>

This block displays list of terms from chosen vocabulary as a list. If you want to change formatting, replace ul and li with something else.

Kasia Wakarecy
http://www.freeformsolutions.ca
IT support for NFPs

Thanks

This is helpfull !!

About this page

Drupal version
Drupal 4.6.x

Archive

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here