This piece of code is to generate a menu or list of links to taxonomy pages. I used it inside a block. I created a view that shows all items under certain taxonomy term.
It is basically a taxonomy menu but you can customized an place it anywhere.
Good link to learn more about embedding your own views: http://groups.drupal.org/node/10129
<?php
$vid = 6; // this is the vocabulary id
$parent = 1679; // this is the parent term
$taxo_tree = taxonomy_get_tree($vid, $parent, $depth = -1, $max_depth = 1);
$items = array();
foreach($taxo_tree as $term){
// This is a better way to get all the nodes under a term, including nodes tagged with child terms.
$count = taxonomy_term_count_nodes($term->tid, $type = 0);
if($count){
$items[] = array("data" => l($term->name, "taxonomy/term/".$term->tid)." [$count]");
}
}
print theme_item_list($items, $title = NULL, $type = 'ul', $attributes = NULL);
?>
Hi,
it's really useful for me as an example of working with views 2,
but I'm sorry I don't understand necessity of views here.
For counting nodes we can use function taxonomy_term_count_nodes.
Thank you so much for your comment. I didn't know there was a
taxonomy_term_count_nodes
function in the API. This makes a lot more sense. I was using the views because I thought it was a good way to get all the nodes including the child nodes, but with the function that you just suggested it is less code and I guess less php processing.
Thanks again.
Post new comment