2011-04-21 72 views
1

我試圖顯示來自節點的2個詞彙表的每個術語,但是我有這種情況。
我有2個詞彙表,像「品牌」和「汽車」的東西。Drupal顯示不同詞彙的所有詞條

  • 品牌
    • 福特
    • BMW
    • 奔馳
  • 汽車
    • 淺藍色
    • 綠色

我的節點必須是即使在寶馬,甚至在「淡藍色」。

在我template.tpl我有這個功能:

function theme479_print_terms($node, $vid = NULL, $ordered_list = TRUE) { 
    $vocabularies = taxonomy_get_vocabularies(); 
    if ($ordered_list) $output .= '<ul>'; //checks to see if you want an ordered list 
    if ($vid) { //checks to see if you've passed a number with vid, prints just that vid 
     $output = '<div class="tags-'. $vid . '">'; 
     foreach($vocabularies as $vocabulary) { 
     if ($vocabulary->vid == $vid) { 
      $terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid); 
      if ($terms) { 
      $links = array(); 
      $output .= '<span class="only-vocabulary-'. $vocabulary->vid . '">'; 
      if ($ordered_list) $output .= '<li class="vocabulary-'. $vocabulary->vid . '">' . $vocabulary->name . ': '; 
      foreach ($terms as $term) { 
       $links[] = '<span class="term-' . $term->tid . '">' . l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description))) .'</span>'; 
      } 
      $output .= implode(', ', $links); 
      if ($ordered_list) $output .= '</li>'; 
      $output .= '</span>'; 
      } 
     } 
     } 
    } 
    else { 
     $output = '<div class="tags">'; 
     foreach($vocabularies as $vocabulary) { 
     if ($vocabularies) { 
      $terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid); 
      if ($terms) { 
      $links = array(); 
      $output .= '<ul class="vocabulary-'. $vocabulary->vid . '">'; 
      if ($ordered_list) $output .= '<li class="vocabulary-'. $vocabulary->vid . '">' . $vocabulary->name . ': '; 
      foreach ($terms as $term) { 
       $links[] = '<span class="term-' . $term->tid . '">' . l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description))) .'</span>'; 
      } 
      $output .= implode(', ', $links); 
      if ($ordered_list) $output .= '</li>'; 
      $output .= '</ul>'; 
      } 
     } 
     } 
    } 
    if ($ordered_list) $output .= '</ul>'; 
    $output .= '</div>'; 
    return $output; 
} 

在我node.tpl,我有這樣的:

print theme_print_terms($node, $unordered_list = TRUE); 

結果如下:

品牌:寶馬
顏色:淺藍色

c我得到如下的東西?

品牌:寶馬
顏色:藍色/淺藍色

因此,與每一位家長的類別。你可以幫我嗎?


編輯:

它可以顯示一個詞彙的所有層次?例如,我有一個詞彙結構(食品>水果> Fruit_with_seeds - >蘋果),但此功能只顯示水果> Fruit_with_seeds)

回答

0

你只需要檢查每個術語的父項。如果找到父項,請將其置於變量中,然後將其前置到鏈接文本中。一種方法來檢查,任期父:

if ($get_parents = taxonomy_get_parents($term->tid, 'tid')) { 
    foreach ($get_parents as $parents) { 
    $parent = $parents->name . '/'; 
    } 
} 

要添加到您的鏈接文字:

$links[] = '<span class="term-' . $term->tid . '">' . l($parent . $term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description))) .'</span>'; 

全部放在一起:(注:這是一個非常討厭的功能,應該將HTML與檢索父項進行分離,以在另一個函數中格式化輸出)。

function theme479_print_terms($node, $vid = NULL, $ordered_list = TRUE) { 
    $vocabularies = taxonomy_get_vocabularies(); 
    if ($ordered_list) $output .= '<ul>'; //checks to see if you want an ordered list 
    if ($vid) { //checks to see if you've passed a number with vid, prints just that vid 
     $output = '<div class="tags-'. $vid . '">'; 
     foreach($vocabularies as $vocabulary) { 
     if ($vocabulary->vid == $vid) { 
      $terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid); 
      if ($terms) { 
      $links = array(); 
      $output .= '<span class="only-vocabulary-'. $vocabulary->vid . '">'; 
      if ($ordered_list) $output .= '<li class="vocabulary-'. $vocabulary->vid . '">' . $vocabulary->name . ': '; 
      foreach ($terms as $term) { 
       // Check for terms parent, if found assign to $parent variable 
       if ($get_parents = taxonomy_get_parents($term->tid, 'tid')) { 
       foreach ($get_parents as $parents) { 
        $parent = $parents->name . '/'; 
       } 
       } 
       $links[] = '<span class="term-' . $term->tid . '">' . l($parent . $term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description))) .'</span>'; 
      } 
      $output .= implode(', ', $links); 
      if ($ordered_list) $output .= '</li>'; 
      $output .= '</span>'; 
      } 
     } 
     } 
    } 
    else { 
     $output = '<div class="tags">'; 
     foreach($vocabularies as $vocabulary) { 
     if ($vocabularies) { 
      $terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid); 
      if ($terms) { 
      $links = array(); 
      $output .= '<ul class="vocabulary-'. $vocabulary->vid . '">'; 
      if ($ordered_list) $output .= '<li class="vocabulary-'. $vocabulary->vid . '">' . $vocabulary->name . ': '; 
      foreach ($terms as $term) { 
       // Check for terms parent, if found assign to $parent variable 
       if ($get_parents = taxonomy_get_parents($term->tid, 'tid')) { 
       foreach ($get_parents as $parents) { 
        $parent = $parents->name . '/'; 
       } 
       } 
       $links[] = '<span class="term-' . $term->tid . '">' . l($parent . $term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description))) .'</span>'; 
      } 
      $output .= implode(', ', $links); 
      if ($ordered_list) $output .= '</li>'; 
      $output .= '</ul>'; 
      } 
     } 
     } 
    } 
    if ($ordered_list) $output .= '</ul>'; 
    $output .= '</div>'; 
    return $output; 
} 
+0

感謝它的工作! – Matteo 2011-04-21 15:33:15

+0

它可以顯示詞彙的所有層次結構?例如我有一個詞彙結構(食品>水果> Fruit_with_seeds - >蘋果),但這個功能只顯示水果> Fruit_with_seeds) – Matteo 2011-04-28 15:12:43