Nested category lists with links to term pages

Last modified: February 19, 2008 - 08:36

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>';
?>

alteration

mikegoodwin - March 22, 2008 - 03:47

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

bullz - June 3, 2008 - 15:48

<?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

jeff h - June 20, 2008 - 13:41

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

Depth > 2

billyboylindien - June 29, 2008 - 17:21

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";
?>

 
 

Drupal is a registered trademark of Dries Buytaert.