Community Documentation

Nested category lists with links to term pages

Last updated February 19, 2008. Created by ugerhard on February 19, 2008.
Log in to edit this page.

The following snippet will generate a nested link list of category terms for a given vocabulary:

<?php
// The ID of the taxonomy vocabulary for which you'd like to create a nested list
$vid = 1;

$depth = 0;
$tree = taxonomy_get_tree($vid);

print
'<ul>';
foreach (
$tree as $term) {
  if (
$term->depth > $depth) {
    print
'<ul>';
   
$depth = $term->depth;
  }
  if (
$term->depth < $depth) {
    print
'</ul>';
   
$depth = $term->depth;
  }
  print
'<li>' . l($term->name, 'taxonomy/term/' . $term->tid) . '</li>';
}
print
'</ul>';
?>

Comments

alteration

Is there a simple alteration that will make the format

<ul>
  <li>Term 1
    <ul>
      <li>Term 1.1</li>
    </ul>
  </li>
  <li>Term 2</li>
</ul>

The list terms with daughter unordered lists should not be closed until the daughter is complete. This method will make the Suckerfish method of menu alteration possible.

-Mike Goodwin
http://www.not2us.net
http://www.redleafmedia.com

Closing lists with more than one level depth

<?php
// The ID of the taxonomy vocabulary for which you'd like to create a nested list
$vid = 1;
$depth = 0;
$tree = taxonomy_get_tree($vid);

print
'<ul class=repos>';
foreach (
$tree as $term) {
   
$count = "(".db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $term->tid)).")";   
    if (
$term->depth > $depth) {
        print
'<ul>';
       
$depth = $term->depth;
    }
    if (
$term->depth < $depth) {   
        for (
$i=($depth - $term->depth);$i>=1;$i--) {
        print
'</ul></li>';
        }
       
$depth = $term->depth;
    }
    print
'<li>' . l($term->name, 'taxonomy/term/' . $term->tid .'/all') . $count .'</li>';
}

print
'</ul>';

?>

Not sure that's right

Try this; I believe it closes the list items in the correct places;

<?php
           
// The ID of the taxonomy vocabulary for which you'd like to create a nested list
           
$vid = 1;

           
$depth = 0;
           
$num_at_depth = 0;
           
$tree = taxonomy_get_tree($vid);

            print
"<ul class=\"menu\">\n<li>";
            foreach (
$tree as $term) {
           
$count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $term->tid));   
              if (
$term->depth > $depth) {
                print
"\n<ul>\n<li>";
               
$depth = $term->depth;
                   
$num_at_depth = 0;
              }
              if (
$term->depth < $depth) {
                print
"</li>\n</ul>\n";
               
$depth = $term->depth;
              }
              if ((
$term->depth == $depth) && ($num_at_depth > 0)) {
                  print
"</li>\n<li>";
                }
              print
l($term->name . ' ('.$count.')', 'taxonomy/term/' . $term->tid);
               
$num_at_depth ++;
            }
            print
"</li>\n</ul>\n";
           
?>

Jeff
________
Drupal Development,
http://marmaladesoul.com

________
Australian Drupal Development
http://marmaladesoul.com

Depth > 2

It seems to not work with depth >2

This seems to be good:

<?php
           
// The ID of the taxonomy vocabulary for which you'd like to create a nested list
           
$vid = 10;

           
$depth = 0;
           
$num_at_depth = 0;
           
$tree = taxonomy_get_tree($vid);

            print
"<ul class=\"menu\">\n<li>";
            foreach (
$tree as $term) {
           
$diffdepth=0;
              if (
$term->depth > $depth) {
                print
"\n<ul>\n<li>";
               
$depth = $term->depth;
                   
$num_at_depth = 0;
              }
              if (
$term->depth < $depth) {
               
$diffdepth= $depth -$term->depth;
                while (
$diffdepth > 0){
                    print
"</li>\n</ul>\n";
                   
$diffdepth -- ;
                }
                   
$depth = $term->depth;
              }
              if ((
$term->depth == $depth) && ($num_at_depth > 0)) {
                  print
"</li>\n<li>";
                }
              print
l($term->name, 'taxonomy/term/' . $term->tid);
               
$num_at_depth ++;
            }
            print
"</li>\n</ul>\n";
?>

Taxonomy Redirect

Just a note here:
I was trying to use this with Taxonomy Redirect module and the path of the taxonomy term must not be run through the proper functions to be rewritten. Therefore, the following line isn't redirected properly:
print l($term->name . ' ('.$count.')', 'taxonomy/term/' . $term->tid);
I ended up doing a custom version of this code to rewrite it myself, but just know that this won't work with Taxonomy Redirect because of the format of that link.

About this page

Drupal version
Drupal 5.x, Drupal 6.x

Reference

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.