如何按层次结构顺序显示分类术语?

发布于 2024-11-27 13:29:00 字数 627 浏览 1 评论 0原文

我有一个词汇表,它按层次结构顺序显示术语,如下所示。

  • 父级1

    • 孩子1
    • 孩子2
  • 父2
    • 孩子1
  • 父母3
  • 父母4
    • 孩子1
    • 孩子2
    • 孩子3

这显示在分类管理器中。

我想创建一个页面,在该页面上调用函数并传递视频并按层次结构顺序显示该词汇表的分类术语,就像在分类管理器中一样。 我使用了以下代码,但它仅显示分类术语,而不是按树顺序。

$vid = 26;
$tree = taxonomy_get_tree($vid);
foreach($tree as $term) {
  $output =  l($term->name, taxonomy_term_path($term));
  if ($term->children) {
    $output .= theme('illogica_category_tree', $term->children);
  }
}

print $output;

对此有什么想法吗?

I have a vocabulary which displays the terms in the hierarchies order like the following.

  • parent1

    • child1
    • child2
  • parent2
    • child1
  • parent3
  • parent4
    • child1
    • child2
    • child3

This displays in taxonomy manager.

I want to create a page on which I call a function and pass the vid and displays the taxonomy terms of that vocabulary in hierarchies order just like in taxonomy manager.
I used the following code but it displays only the taxonomy terms not in a tree order.

$vid = 26;
$tree = taxonomy_get_tree($vid);
foreach($tree as $term) {
  $output =  l($term->name, taxonomy_term_path($term));
  if ($term->children) {
    $output .= theme('illogica_category_tree', $term->children);
  }
}

print $output;

Any idea about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

別甾虛僞 2024-12-04 13:29:00

我用这个:

$tree = extranet_get_nested_tree((int)$vid, null, $tid);

用这个:

function extranet_get_nested_tree($terms = array(), $max_depth = NULL, $parent = 0, $parents_index = array(), $depth = 0) {
  if (is_int($terms)) {
    $terms = taxonomy_get_tree($terms);
  }

  foreach($terms as $term) {
    foreach($term->parents as $term_parent) {
      if ($term_parent == $parent) {
        $return[$term->tid] = $term;
      }
      else {
        $parents_index[$term_parent][$term->tid] = $term;
      }
    }
  }

  foreach($return as &$term) {
    if (isset($parents_index[$term->tid]) && (is_null($max_depth) || $depth <     $max_depth)) {
      $term->children = extranet_get_nested_tree($parents_index[$term->tid],     $max_depth, $term->tid, $parents_index, $depth + 1);
    }
  }
  return $return;
}

并打印出来:

    function extranet_output_nested_tree($tree) {
    if (count($tree)) {
        $output = '<ul class="nested-taxonomy-tree">';
        foreach ($tree as $term) {
            $output .= '<li class="taxonomy-term">';
            $output .= t($term->name); //, taxonomy_term_path($term));
            if ($term->children) {
                $output .= extranet_output_nested_tree( $term->children);
            }
            $output .= '</li>';
        }
        $output .= '</ul>';
    }
    return $output;
}

享受!

I use this:

$tree = extranet_get_nested_tree((int)$vid, null, $tid);

With this:

function extranet_get_nested_tree($terms = array(), $max_depth = NULL, $parent = 0, $parents_index = array(), $depth = 0) {
  if (is_int($terms)) {
    $terms = taxonomy_get_tree($terms);
  }

  foreach($terms as $term) {
    foreach($term->parents as $term_parent) {
      if ($term_parent == $parent) {
        $return[$term->tid] = $term;
      }
      else {
        $parents_index[$term_parent][$term->tid] = $term;
      }
    }
  }

  foreach($return as &$term) {
    if (isset($parents_index[$term->tid]) && (is_null($max_depth) || $depth <     $max_depth)) {
      $term->children = extranet_get_nested_tree($parents_index[$term->tid],     $max_depth, $term->tid, $parents_index, $depth + 1);
    }
  }
  return $return;
}

And to print it out:

    function extranet_output_nested_tree($tree) {
    if (count($tree)) {
        $output = '<ul class="nested-taxonomy-tree">';
        foreach ($tree as $term) {
            $output .= '<li class="taxonomy-term">';
            $output .= t($term->name); //, taxonomy_term_path($term));
            if ($term->children) {
                $output .= extranet_output_nested_tree( $term->children);
            }
            $output .= '</li>';
        }
        $output .= '</ul>';
    }
    return $output;
}

Enjoy!

神经暖 2024-12-04 13:29:00

taxonomy_get_tree() 应该以正确的顺序返回术语,但数组是扁平的而不是嵌套的。请参阅 taxonomy_get_tree()更多信息和一些示例函数来获取嵌套数组。

taxonomy_get_tree() should return the terms in the correct order, but the array is flat rather than nested. See taxonomy_get_tree() for more information and some sample functions to get a nested array.

勿忘心安 2024-12-04 13:29:00

这是解决方案`

  foreach($terms as $term) {
    foreach($term->parents as $term_parent) {
      if ($term_parent == $parent) {
        $return[$term->tid] = $term;
      }
      else {
        $parents_index[$term_parent][$term->tid] = $term;
      }
    }
  }

  foreach($return as &$term) {
    if (isset($parents_index[$term->tid]) && (is_null($max_depth) || $depth < $max_depth)) {
      $term->children = taxonomy_get_nested_tree($parents_index[$term->tid], $max_depth, $term->tid, $parents_index, $depth + 1);
    }
  }

  return $return;
}

function output_taxonomy_nested_tree($tree) {
    if (count($tree)) {
        $output = '<ul class="nav navbar-nav">';
        foreach ($tree as $term) {            
             $output .= '<li class="taxonomy-term">' . l($term->name, "taxonomy/term/". $term->tid);


            if ($term->children) {
                $output .= output_taxonomy_nested_tree($term->children);
            }
            $output .= '</li>';
        }
        $output .= '</ul>';
    }
    return $output;
}
$tree= taxonomy_get_nested_tree(2,10);
$output=output_taxonomy_nested_tree($tree);
echo $output;`

如果您想在特定区域打印它,只需将输出分配给预处理器函数中的变量,然后在页面 tpl 文件中打印该变量。

$output=output_taxonomy_nested_tree($tree);
回显$输出;`

$variables['output'] .=output_taxonomy_nested_tree($tree);

将该行放入

<?php print $output;?>

您的页面 tpl 文件中

Here is the solution `

  foreach($terms as $term) {
    foreach($term->parents as $term_parent) {
      if ($term_parent == $parent) {
        $return[$term->tid] = $term;
      }
      else {
        $parents_index[$term_parent][$term->tid] = $term;
      }
    }
  }

  foreach($return as &$term) {
    if (isset($parents_index[$term->tid]) && (is_null($max_depth) || $depth < $max_depth)) {
      $term->children = taxonomy_get_nested_tree($parents_index[$term->tid], $max_depth, $term->tid, $parents_index, $depth + 1);
    }
  }

  return $return;
}

function output_taxonomy_nested_tree($tree) {
    if (count($tree)) {
        $output = '<ul class="nav navbar-nav">';
        foreach ($tree as $term) {            
             $output .= '<li class="taxonomy-term">' . l($term->name, "taxonomy/term/". $term->tid);


            if ($term->children) {
                $output .= output_taxonomy_nested_tree($term->children);
            }
            $output .= '</li>';
        }
        $output .= '</ul>';
    }
    return $output;
}
$tree= taxonomy_get_nested_tree(2,10);
$output=output_taxonomy_nested_tree($tree);
echo $output;`

If you want to print it in the specific region you needed just assign the output to a variable in preprocessor function and print the variable in the page tpl file.

$output=output_taxonomy_nested_tree($tree);
echo $output;`

to

$variables['output'] .=output_taxonomy_nested_tree($tree);

place the line

<?php print $output;?>

in your page tpl file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文