开发者

Taxonomy: Top level term view?

Is it possible to create a view to o开发者_运维百科nly see the top level terms in a vocabulary? I can't seem to get it to stop listing all terms, using a vocabulary ID argument. I just want to see the top level parents.


This blog post outlines how to do it: http://www.raisedeyebrow.com/2011/01/show-only-top-level-terms-in-a-term-type-drupal-view/

Essentially you need to add a relationship of term parent. Then add a filter for term name and select is empty (null) using the parent relationship. Effectively only showing terms that have no parent.


You can add a filter of Taxonomy: Term ID and manually choose which terms to show (may be tedious if you have a large vocabulary).

OR

You could add a template file for a field in your view to decide what terms to show. For example, in your view, you could simply add a field of Taxonomy: Term ID. Copy views-view-field.tpl.php to your theme folder from the views module directory (under theme). Go to "Theme information" under "Basic settings" and find a suitable name for the template and create a new file using that name. For example, mine was views-view-field--tax--tid.tpl.php.

To only show term names of the terms that are the top level of a vocabulary, use the following (or similar) in your new template file:

<?php 
  if (count(taxonomy_get_parents($output, $key = 'tid')) == 0) {
    $term = taxonomy_get_term($output, $reset = FALSE);
    print $term->name;
  }
?>


Yes its possible but not sure with views.Below is one way to get the top level terms in vocabulary.

$tree = taxonomy_get_tree($vocabulary_id, 0, -1, 1);

taxonomy_get_tree returns a flat array of terms so you can use that while printing.

Regards, Chintan.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜