Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Drupal-仅当存在子项时才显示当前项的子项-需要修改代码_Drupal_Drupal Taxonomy - Fatal编程技术网

Drupal-仅当存在子项时才显示当前项的子项-需要修改代码

Drupal-仅当存在子项时才显示当前项的子项-需要修改代码,drupal,drupal-taxonomy,Drupal,Drupal Taxonomy,在解决了在块中显示给定分类术语的子术语的问题后,我最终偶然发现了一段代码,它完全符合我的要求 根据说明,我已将以下内容添加到我的template.php中 function themename_child_terms($vid = 1) { if(arg(0) == 'taxonomy' && arg(1) == 'term') { $children = taxonomy_get_children(arg(2), $vid); if(!$child

在解决了在块中显示给定分类术语的子术语的问题后,我最终偶然发现了一段代码,它完全符合我的要求

根据说明,我已将以下内容添加到我的template.php中

function themename_child_terms($vid = 1) {
  if(arg(0) == 'taxonomy' && arg(1) == 'term') {   
    $children = taxonomy_get_children(arg(2), $vid);
      if(!$children) {
        $custom_parent = taxonomy_get_parents(arg(2));
          $parent_tree = array();
          foreach ($custom_parent as $custom_child => $key) {
            $parent_tree = taxonomy_get_tree($vid, $key->tid);
          }
          $children = $parent_tree;
      }

    $output = '<ul>';
    foreach ($children as $term) {
      $output .= '<li>';
      $output .= l($term->name, 'taxonomy/term/' . $term->tid);
      $output .= '</li>';
    }
    $output .= '</ul>';

    return $output;
  }
}
函数名称子项($vid=1){
如果(arg(0)='taxonomy'&&arg(1)='term'){
$children=taxonomy\u get\u children(arg(2),$vid);
如果(!$儿童){
$custom_parent=taxonomy_get_parent(arg(2));
$parent_tree=array();
foreach($custom\u parent作为$custom\u child=>$key){
$parent\u tree=taxonomy\u get\u tree($vid,$key->tid);
}
$children=$parent_树;
}
$output='
    '; foreach($子项作为$term){ $output.='
  • '; $output.=l($term->name,'taxonomy/term/'。$term->tid); $output.='
  • '; } $output.='
'; 返回$output; } }
然后我创建了一个块并添加:

<?php // $vid is the vocabulary id.
    print themename_child_terms($vid = 1);
?>

这完美地显示了当前术语的子术语。但是,它显示父术语下存在的所有术语,即使没有使用该术语的内容

e、 g。 查看包含学期1中所有项目的页面,我得到

儿童1
儿童2
儿童3

在块中正确列出。但是,如果没有标记为“child 3”的内容,它仍然会在块中显示该术语。这不是很有用,因为它链接到一个空的学期页面。 我如何修改代码以仅显示实际具有与之关联的项的子项。因此,如果没有标记为“child3”的孩子,那么这个词就不会出现。这是一个简单的修改吗

感谢您提供的任何解决方案

尼克


使用Drupal 6

感谢用户Jerdigit在Drupal.stackexchange上发布了以下回复。工作完美


嗯。。。我想试试这样的东西:

// Avoid unnecessary "Invalid foreach" errors showing up in the log:
if (!empty($children)) {
  // If not empty, run the foreach loop:
  foreach ($children as $term) {
    // Then check to see if any nodes exist for that term id:
    $number_of_nodes = taxonomy_term_count_nodes($term->tid);
    // If there ARE nodes...
    if ($number_of_nodes > 0) {
      // ... then add them to the output:
      $output .= '<li>';
      $output .= l($term->name, 'taxonomy/term/' . $term->tid);
      $output .= '</li>';
    }
  }
}
更改代码的这一部分:

foreach ($children as $term) {
  $output .= '<li>';
  $output .= l($term->name, 'taxonomy/term/' . $term->tid);
  $output .= '</li>';
}
foreach($children作为$term){
$output.='
  • '; $output.=l($term->name,'taxonomy/term/'。$term->tid); $output.='
  • '; }
    对这样的事情:

    // Avoid unnecessary "Invalid foreach" errors showing up in the log:
    if (!empty($children)) {
      // If not empty, run the foreach loop:
      foreach ($children as $term) {
        // Then check to see if any nodes exist for that term id:
        $number_of_nodes = taxonomy_term_count_nodes($term->tid);
        // If there ARE nodes...
        if ($number_of_nodes > 0) {
          // ... then add them to the output:
          $output .= '<li>';
          $output .= l($term->name, 'taxonomy/term/' . $term->tid);
          $output .= '</li>';
        }
      }
    }
    
    //避免日志中出现不必要的“Invalid foreach”错误:
    如果(!空($children)){
    //如果不是空的,则运行foreach循环:
    foreach($子项作为$term){
    //然后检查该术语id是否存在任何节点:
    $number\u of_nodes=分类法\u术语\u计数\u节点($term->tid);
    //如果有节点。。。
    if($节点的数量>0){
    //…然后将它们添加到输出:
    $output.='
  • '; $output.=l($term->name,'taxonomy/term/'。$term->tid); $output.='
  • '; } } }
    希望这有助于……:)