【问题标题】:how to display the taxonomy terms in hierarchies order?如何按层次结构顺序显示分类术语?
【发布时间】:2011-08-02 05:50:44
【问题描述】:

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

  • 父母1

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

这显示在分类管理器中。

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

$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;

对此有什么想法吗?

【问题讨论】:

    标签: drupal drupal-6 drupal-taxonomy


    【解决方案1】:

    我用这个:

    $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;
    }
    

    享受吧!

    【讨论】:

    • 优秀的解决方案!我没有“自己的自行车”就拿走了它。谢谢你,朋友!我刚刚将它从 hook_menu 包装到我的函数中
    【解决方案2】:

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

    【讨论】:

    • 在发帖之前我已经检查过了。我在那里没有找到任何解决方案。
    • 至少有一个令人满意的解决方案 ATM。检查以 O(n) 时间运行的 api.drupal.org/comment/52303#comment-52303 示例。
    【解决方案3】:

    这里是解决方案`

      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 文件中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      相关资源
      最近更新 更多