Drupal显示不同词汇的所有术语
我试图显示一个节点中 2 个词汇表的每个术语,但我遇到了这种情况。
我有两个词汇,比如“品牌”和“汽车”。
- 品牌
- 福特
- 宝马
- 梅赛德斯
- 汽车
- 蓝色
- 浅蓝色
- 红色
- 绿色
我的节点必须在 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);
结果如下:
品牌:BMW
颜色:浅蓝色
我怎样才能得到类似下面的东西?
品牌: 宝马
颜色:蓝色/浅蓝色
每个父类别也是如此。你可以帮我吗?
编辑:
可以显示词汇表的所有层次结构吗?例如,我有一个具有这种结构的词汇表(食品>水果>Fruit_with_seeds ->苹果),但此功能仅显示Fruits>Fruit_with_seeds)
I'm trying to show every terms of 2 vocabularies from a node, but I have this situation.
I have 2 vocabularies, somethings like "brands" and "cars".
- Brands
- Ford
- BMW
- Mercedes
- Cars
- Blue
- Light Blue
- Red
- Green
My node has to be even in BMW and even in "Light Blue."
In my template.tpl i have this function:
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;
}
In my node.tpl, I have this:
print theme_print_terms($node, $unordered_list = TRUE);
The result is the following:
Brands: BMW
Colors: Light Blue
How can I get something like the following?
Brands: BMW
Colors: Blue/Light Blue
So with every parent categories. May you help me?
EDIT:
it's possible to show all hierarchy of a vocabulary? for example i have a vocabulary with this structure (Foods>Fruits>Fruit_with_seeds ->apple) but this function only show Fruits>Fruit_with_seeds)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需检查每个术语的父术语即可。如果找到父术语,请将其放入变量中,然后将其添加到链接文本之前。检查术语父项的方法:
添加到链接文本:
全部放在一起:(注意:这是一个看起来非常令人讨厌的函数,HTML 确实应该与术语父项的检索分开,以格式化输出另一个函数)。
You simply need to check each term for parent terms. If a parent term is found place it in a variable and then prepend it to your link text. A way to check for a term parent:
To add to your link text:
All put together: (note: This is a pretty nasty looking function, the HTML should really be separated from the retrieval of term parents to format output in another function).