Taxonomy term list with subterms for Drupal
February 12th, 2008 tela
As a complement to the taxonomy term list for Drupal I posted a while ago I am posting this new code that will work for taxonomies with terms and subterms.
This was something you asked for and I actually just had to write it for the last website I worked on.
The code will output a list of the taxonomy terms with a tree structure that shows the subterms. It shows the number of nodes for each term.
<?php
$vid = 1; /* <---- put correct vocabulary ID here */
$terms = taxonomy_get_tree($vid);
print "<ul>";
foreach ( $terms as $term ) {
$tcount = taxonomy_term_count_nodes($term->tid);
$children_terms = taxonomy_get_children($term->tid);
if ($term->depth == 0) {
print "<li>";
print l($term->name." (".$tcount.")",'taxonomy/term/'.$term->tid, array('title' => $tcount." items in ".$term->name));
if ($children_terms) {
print "<ul>";
foreach ( $children_terms as $children_term ) {
$t_children_count = taxonomy_term_count_nodes($children_term->tid);
print "<li>";
print l($children_term->name." (".$t_children_count.")",'taxonomy/term/'.$children_term->tid, array('title' => $t_children_count." items in ".$children_term->name));
print "</li>";
}/* end foreach children */
print "</ul>";
}
print "</li>";
}
} /* end foreach */
print "</ul>";
?>
Entry Filed under: Drupal


10 Comments Add your own
1. Matt | February 27th, 2008 at 3:32 pm
This is excellent, many thanks! Took me ages to find it, but it does exactly what I want with really minimal faff- cheers!
2. drupalyte | March 7th, 2008 at 6:09 am
This is great! Is there any way to get a long list of terms and subterms to display in two even columns? thanks for a great tutorial and info.
3. Marcin Pajdzik | March 20th, 2008 at 4:27 pm
drupalyte:
I have done it with taxonomy menu and sliced menu modules. You will get a separate block for each level of your taxonomy.
4. Paul | April 25th, 2008 at 3:26 pm
Helpful snippet!
Just one question:
How can I remove the bracketed ‘content count’ number after each term? I’d prefer the term on it’s own, with no number after it.
Do I just need to delete some of the code?
5. Drupal Berlin | May 3rd, 2008 at 12:43 pm
@paul
Try taking out this first:
$tcount.(That is one dollar and one dot!!!)
and see what happens. You’ll be able to figure out the rest yourself…
6. Joe | June 10th, 2008 at 5:53 pm
The code for this post is not displayed.
7. Evan Donovan | June 20th, 2008 at 10:21 pm
Has the code been removed for this snippet? This is exactly what I would need, if I can find it.
8. tela | June 21st, 2008 at 8:40 am
Hello,
This post has been hacked and the code has been removed.
I’ll put it back as soon as I can.
9. lim | July 2nd, 2008 at 6:31 pm
Hi.
Can you place the codes to list with subterm?
I believe drupal 6.2 will resolve the security issue.
10. tela | July 3rd, 2008 at 11:45 am
Hi,
I put the code back online.
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed