Drupal文档教程翻译 - 在节点中按照不同词汇表显示词条

logo-drupal-documentation本站原创编译,转载请给面子,尊重劳动果实,欢迎交流指正。

各位早,风挺大,天挺蓝。有哪位朋友最近看过哪个什么晚会上一穿的人模狗样的诗人吟诗刚吟了两句就死活想不起来词的那个视频没?突然想到的。Drupal文档中文翻译时间,仍是有关节点内容与词汇表、词条之间的那点事。之前一篇里面,我们了解了怎样在节点中显示完整的词条层级路径,今天的话题类似,来看看怎样按照不同的词汇表来输出当前节点的词条。

节点中的词条是由node.tpl.php模板文件中的$terms输出的Drupal默认会将所有词条并排显示出来,而我们希望得到下面这样的效果:

  • Topic: foo, bar
  • Tags: bat

也就是将话题分类与Tag区别输出;当然分类或Tag这样的词汇表是我们根据自己的需求设定的,这里只是个常规的例子;这也确实是个很常见的需求,很少有人希望将分类与Tag混在一起没有任何区别的显示给访问者。

下面的代码可以帮我们实现这样的需求;我们还可以将指定词汇表的id,也就是vid传递给该函数,那么页面中只会输出该词汇表中的词条;另外一个布尔值参数$ordered_list允许我们指定ture或false,来设定词条是否以有序列表的形式输出。将代码添加到当前主题的template.php文件中,记得添加的时候去掉最外面的php封装标签,并将所有的“yourthemename”替换为当前主题名称:

<?php
function yourthemename_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.php文件中添加相应代码来输出词条。最基本的方式是这样的:

<?php
print yourthemename_print_terms($node);
?>

 

注意,这些使用在主题文件的代码是需要有外层PHP封装标签的;另外记得改下yourthemename主题名称。

下面是两段代码的作用是,如果当前内容不是引文(teaser),则显示以逗号分隔的词条列表,否则以有序列表的方式输出词条列表。

<?php
if ($terms && !$teaser) { print yourthemename_print_terms($node, $vid = NULL, $unordered_list = TRUE);}
?>

 

<?php
if ($terms && $teaser) { print yourthemename_print_terms($node, $vid = NULL, $unordered_list = FALSE);}
?>

 

下面的代码只输出id为3的词汇表中的词条:

<?php
print yourthemename_print_terms($node, $vid = 3, $unordered_list = TRUE);
?>

 

通过下面的代码,我们可以将词条输出到区块中,并使该区块只在节点页面中可见:

<?php
$nid = arg(1);
print yourthemename_print_terms($nid);
?>

 

另外,原文档的评论中,很多用户贡献了针对其他特定需求的代码改写,推荐围观。

译者:Viiiix7210

如需转载,请注明官方英文文档及本人译文的出处,谢谢。查看英文原文: Display taxonomy terms broken out by vocabulary


收藏与分享

添加评论


Security code
换一张图