2015-10-13 41 views
0

我想列出一個詞彙術語如下格式上市的Drupal taxanomy方面

<ul id="vid1"> 
<li>term1</li> 
<li>term2 
    <ul> 
    <li>term2-child1</li> 
    <li>term2-child2</li> 
    </ul> 
</li> 
<li>term3 
    <ul> 
    <li>term3-child1</li> 
    <li>term3-child2</li> 
    </ul> 
</li> 
</ul> 

我如何修改下面的代碼,以滿足我的要求

<?php 
$vid = 1; 
$terms = taxonomy_get_tree($vid); 
print_r($terms); 
echo "<ul>"; 
foreach ($terms as $term) { 
$count = db_query("SELECT COUNT(nid) FROM {taxonomy_index} WHERE tid = :tid ", array(':tid' => $term->tid))->fetchField(); 
print "<li>".$term->name." (".$count.")</li>"; 
} 
echo "</ul>"; 
?> 

回答

0
$vid = 1; 

$tree = taxonomy_get_tree($vid); 
print '<ul id="vid1">'; 

foreach($tree as $key=>$term) { 
if($term->parents[0]==0) { 
    print '<li>'.$term->name; 

    $childrens = taxonomy_get_children($term->tid); 
    print '<ul>'; 

    foreach($childrens as $children) { 
    print '<li>'.$children->name.'</li>'; 
    } 
    print '</ul></li>'; 
} 
}; 

print '</ul>'; 
+0

謝謝它的工作原理。我如何將它們鏈接到相應的條款? – troshan

+0

,類似於