PHP Snippet to List Child Terms (n) of a parent term

nisguy - October 10, 2007 - 21:44

I need to Print a list of child terms in a term with the number of nodes in that term in parentheses. For example, if these were my categories:

  • Vocabulary = Food ($vid = 1)
    • Parent Term = Fruit ($tid = 1)
      • Child Term = Apples ($tid = 2)
      • Child Term = Oranges ($tid = 3)
      • Child Term = Grapes ($tid = 4)
      • Child Term = Bananas ($tid = 5)

I would want it to display like this (number is how many nodes in the child term):

Types of Fruit

  • Apples (6)
  • Oranges (8)
  • Grapes (5)
  • Bananas (2)

But the closest I can get is with this snippet:

<?php
$tid
= 1;
$items = array();
$children = taxonomy_get_children($tid);
foreach (
$children as $child ) {
$items[] = l($child->name, 'taxonomy/term/'. $child->tid). ' ('.count($children).')';
}
print
theme('item_list', $items);
?>

But the number in parentheses is the number of child terms, instead of the number of nodes. Like this:

Types of Fruit

  • Apples(4)
  • Oranges (4)
  • Grapes (4)
  • Bananas (4)

Help would be great.

Need to count the nodes and not the children

nevets - October 10, 2007 - 22:26

Something like this should do the trick

<?php
$tid
= 1;
$items = array();
$children = taxonomy_get_children($tid);
foreach (
$children as $child ) {
$count = db_result(db_query("SELECT COUNT(nid) as count FROM {term_node} WHERE tid = %d", $child->tid));
$items[] = l($child->name, 'taxonomy/term/'. $child->tid). ' ('.$count.')';
}
print
theme('item_list', $items);
?>

[ Edited to fix code: nevets ]

Thanks for the snippet.

nisguy - October 11, 2007 - 17:25

Thanks for the snippet. When I try it, it shows only zeros for all the counts. Is something wrong, or was my original post too ambiguous?

If you click on a link for a term with a zero count

nevets - October 11, 2007 - 18:08

If you click on a link for a term with a zero count do you see any nodes listed? If yes, then it would help if you posted the code you are currently using.

Yes, when I click the links,

nisguy - October 11, 2007 - 20:35

Yes, when I click the links, the correct nodes are listed. This is what I'm actually using:

<?php
$tid
= 71;
$items = array();
$children = taxonomy_get_children($tid);
foreach (
$children as $child ) {
$items[] = l($child->name, 'taxonomy/term/'. $child->tid);
}
print
theme('item_list', $items);
?>

My Terms setup as

Vocab

  • Term (hospital)
    • sub-term1 (physician)
      • bunch of nodes (medical reports)
    • sub-term2 (physician 2)
      • bunch of nodes (medical reports)

I want to be able to create a list on each hospital page that lists the Physicians with a link to a list of their reports. For Example:

Candler County Hospital

might help?

strawman - October 11, 2007 - 21:27

check this link - http://drupal.org/node/177152

not sure if this helps but the code there will give a full list of the vocabulary's terms with children and the number of nodes in each term.
You end up with a list like...

  • Hotel Something
  • - (node title w/link)

  • Adventure Tours (0)
  • Beauty Services & Spa(0)
  • Bed and Breakfasts(0)
  • Bridal Shows (0)
  • Cakes (2)
    • Something Bakery
    • Trial Cakes
  • Carriages(0)
  • Catering (0)
  • Ceremony Locations (0)
  • Chocolates & Sweets (1)
    • The initial list item gives is the name of the term with the amount of nodes in it and a link to that term page while the indent list terms are the title of the nodes with link to the nodes.

      Maybe this will help?

    That's close Strawman,

    nisguy - October 12, 2007 - 02:28

    That's close Strawman, assuming that the vocabulary term in your example is 'Hotel' and the term is 'Hotel Something', I would need to print the sub-terms:

    • Adventure Tours (0)
    • Beauty Services & Spa(0)
    • Bed and Breakfasts(0)
    • Bridal Shows (0)
    • Cakes (2)
    • Carriages(0)
    • Catering (0)
    • Ceremony Locations (0)
    • Chocolates & Sweets (1)

    I've been fiddling with different combinations and rooting throught the API for the last couple days. The part that is unconventional is that my terms contain no nodes. Only my sub-terms have the nodes. This helps me keep all 20+ terms and 500+ sub-terms organized for my 1500+ nodes. Very Frustrating!

    Solution

    nisguy - October 12, 2007 - 02:41

    I had to change $child->term to $child->tid in the $count variable. Now it prints the number of nodes in the sub-category.

    <?php
    $tid
    = 71;
    $items = array();
    $children = taxonomy_get_children($tid);
    foreach (
    $children as $child ) {
    $count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $child->tid));
    $items[] = l($child->name, 'taxonomy/term/'. $child->tid). ' ('.$count.') ';
    }

    print
    theme('item_list', $items);
    ?>

    thanks all!

    Is there a module for this

    Summit - February 6, 2008 - 09:25

    Is there a module for this please?
    EDIT: I found panels_taxonomy as a module for this.
    EDIT 2: May be this will work:

    taxonomy_render_nodes(taxonomy_select_nodes($array_with_tids, 'or', 0, TRUE));

    Does somebody have a solution to show the node titles and teasers, instead of the count of the nodes?
    Thanks a lot for your reply!
    greetings,
    Martijn

    Views Module? That is my

    nisguy - February 7, 2008 - 11:06

    Views Module? That is my obvious answer unless I misunderstood.

    Hi, I succeeded with this. I

    Summit - February 7, 2008 - 13:54

    Hi, I succeeded with this. I made : http://drupal.org/node/128085#comment-719496
    Views was not possible, because I need the results in a already available module panels_taxonomy.
    Greetings,
    Martijn

     
     

    Drupal is a registered trademark of Dries Buytaert.