Сниппет выводит дерево иерархии таксономии.
Но есть ошибка - все адреса получаются типа mysite.ru/rep/term/17...
Подскажите - как /rep/ убрать?
<?php
// The ID of the taxonomy vocabulary for which you'd like to create a nested list
$vid = 2;
$debug = false;
// Dado un elemento reconstruido con sus children enlazados,
// busca entre sus hijos y va añadiendo los ids estos útlimos
// al array buffer pasado por referencia.
function fill_children_ids_recursive(&$term_rebuilt, &$children_ids) {
global $debug;
if (
$term_rebuilt["children_count"]>0) {
for ($i=0;$i<$term_rebuilt["children_count"];$i++) {
if ($debug) {
print_r($term_rebuilt["children"]);
}
if (!in_array($term_rebuilt["children"][$i]["id"], $children_ids)) {
$children_ids[]=$term_rebuilt["children"][$i]["id"];
}
fill_children_ids_recursive($term_rebuilt["children"][$i], $children_ids);
}
}
}
// Los terminos que nos devuelve esta función no tienen hijos enlazados
// con lo que nos vemos obligados a reconstruir la matriz de términos
// para enlazar también los hijos.
$tree = taxonomy_get_tree($vid);
$terms = array();
// Reconstruimos el array de terminos
// añadiendo algunos campos que nos hacen falta
// y que no están establecidos por defecto.
foreach ($tree as &$term) {
$terms[] = array(
"id"=>$term->tid,
"name"=>$term->name,
"parents"=>$term->parents,
"depth"=>$term->depth,
"children"=>array(),
"children_count"=>0,
"node_count"=>0,
"global_node_count"=>0
);
}
foreach ($terms as $key => &$term_rebuilt) {
for ($x=0;$x<sizeof($term_rebuilt["parents"]);$x++) {
$parent_tid = $term_rebuilt["parents"][$x];
$idx=0;
$not_found = true;
while($not_found&&($idx<sizeof($terms))) {
if ($terms[$idx]["id"]==$parent_tid) {
if ($debug) {
/*
echo "<pre>found: \$idx = $idx\n";
print_r($terms[$idx]);
echo "</pre>";
*/
}
$terms[$idx]["children"][]=&$term_rebuilt;
$terms[$idx]["children_count"]++;
$not_found = false;
}
$idx++;
}
}
}
if (
$debug) {
echo "<pre>Arbol de elementos recontruidos\n\n";
print_r($terms);
echo "</pre>";
}
// Creación de la lista HTML.
print "<ul><li>";
$depth = 2;
foreach($terms as $index => &$term_rebuilt) {
// Para cada elemento reconstruido se buscan los ids de los
// hijos, no importa a qué nivel de profundad.
$children_ids = array();
fill_children_ids_recursive($term_rebuilt, $children_ids);
// Con los ids de los hijos consultamos para que nos devuelva
// el número de nodos que cuelgan tanto directamente de el cómo
// de sus hijos.
$sql_count = "SELECT COUNT(nid) FROM {term_node} WHERE tid = '".$term_rebuilt["id"]."' ";
if (sizeof($children_ids)>0) {
//print_r($children_ids);
$sql_count .= " OR tid = '";
$sql_count .= join("' OR tid = '",$children_ids);
$sql_count .= "'";
}
if ($debug) {
echo "<pre>SQL for term: ".$term_rebuilt["name"]." -> ".$sql_count."\n</pre>";
}
$term_rebuilt["global_node_count"] = db_result(db_query($sql_count));
// Se expulsa la lista.
if ($term_rebuilt["depth"] > $depth) {
print '<ul>';
$depth = $term_rebuilt["depth"];
}
if ($term_rebuilt["depth"] < $depth) {
for ($i=($depth - $term_rebuilt["depth"]);$i>=1;$i--) {
print '</ul></li>';
}
$depth = $term_rebuilt["depth"];
}
print '<li>' . l($term_rebuilt["name"], 'rep/term/' .$term_rebuilt["id"]) . " (".$term_rebuilt["global_node_count"].")";
}
for (
$i=$depth; $i>=0; $i--)
{
print "</li>\n</ul>\n";
}
if (
$debug) {
echo "<pre>Arbol original, obtenido por: taxonomy_get_tree\n\n";
print_r($terms);
echo "<pre>";
}
?>
ЗЫ. И еще вопрос - как ограничить вывод для конкретного блока только 4 первыми терминами верхнего уровня с подтерминами?
Комментарии
А просто поиск по коду сниппета сделать религия не позволяет ?
print '<li>' . l($term_rebuilt["name"], 'rep/term/' .$term_rebuilt["id"]) . " (".$term_rebuilt["global_node_count"].")";
Точно. Спасибо. На глаза понадеялся - вроде просмотрел - и пропустил, ссори.
А второй вопрос - как выводить только некоторые термины? Например 4 первых группы?