Abstract
The approach described here is intended to create an overview of the hierarchy of a taxonomy & showing the related nodes. The layout of the nodes is defined by the associated view (see below). This could be useful e.g. for a list of all downloads of a page.
Example
Term 1 with Depth 0
- node one of term 1, layout depending on view-settings
- node two of term 1, layout depending on view-settings
- etc., number depending on view-settings
Term 2 with Depth 0
Term 3 with Depth 1, child of Term 2
- nodes of term 3, layout depending on view-settings
Term 4 with Depth 0
- nodes of term 4, layout depending on view-settings
Term 5 with Depth 1, child of Term 4
- nodes of term 5, layout depending on view-settings
Example: http://www.3dcenter.org/downloads
Steps to be taken
1. The Vocabulary
Create a new (optional: hierarchically) taxonomy or use an existing taxonomy. Remember its ID. It will be inserted in the PHP snippet shown in step 3.
2. The View
Create a new view for the nodes that use the vocabulary of step 1. As Argument Type for this view, set Taxonomy: Term Name. Remember the views name. It will be inserted in the PHP snippet shown below.
3. The php Snippet
This snippet is for Drupal 5 and views 1. For Drupal 6 and views 2 see comment of stone_d below.
Create new content or use existing content with PHP as input format. Then paste the following code and enter your values of step 1 & 2 in lines 2 & 5:
/* Enter the ID of the vocabulary which terms you want to show as headlines */
$vocabulary_id = "0";
/* Enter the name of the view that should show the nodes */
$view_name = "YOUR_VIEW";
/* Only edit if you know what you're doing */
$view = views_get_view($view_name);
if (!$view) {
drupal_not_found();
exit;
}
foreach(taxonomy_get_tree($vocabulary_id,0,-1,1) as $value) {
print t("<div style='margin-bottom: 4em;'><h3>" . $value->name . "</h3>");
print views_build_view('embed', $view, array($value->name), FALSE, $view->nodes_per_block);
if ( taxonomy_get_children($value->tid) ) {
print t("<div style='margin-left: 2em;'>");
foreach( taxonomy_get_children($value->tid) as $child ) {
print t("<h4 style='margin-top:2em;'>" . $child->name . "</h4>");
print views_build_view('embed', $view, array($child->name), FALSE, $view->nodes_per_block);
}
print t("</div>");
}
print t("</div>");
}
Known issues
- Currently the code only support terms of the first two depths (the modifications to support unlimited depths shouldn't be hard to do)
- Hard coded CSS styles: I suggest to exchange them with your CSS classes.
Comments
View showing taxonomy terms with related nodes
This snippet does exactly what I need, a list of events at each of several facilities.
I needed to make one important change.
As given, it displays the whole content of an event node. But the view associated with my block wants only a few fields to show.
In the line that starts
print views_build_view('embed'
I changes 'embed' to 'block'The parameters for
views_build_view
are explained here: http://drupal.org/node/99721A solution for Drupal 6 and Views2
thank your for that snippet, u saved my day *hehe
I just trimmed it to drupal 6 with views2 and selected "formatted layout with rows" for my view - works perfect!
stone_d - does the above
stone_d - does the above actually work for Drupal 6? I get the terms as headers but underneath them I get all content rather than the content that has been assigned that term.
"View with taxonomy arguments, presented all on the same page: Terms as headlines, displayed hierarchically" - this is what I want - any other way of getting it? I am a Drupal newb unfortunately...
Thanks
I’ve added I third depth to the code and also made it to hide terms without content. It’s not elegant, but it works. I also remove the t() function from the tags.
This is helpful
This is the first view I've been successful in getting to work on this site that's even close to what I need. However, it still does not correctly display the "child" categories (my vocabulary is made up of product categories and subcategories). It displays the correct contents of each specific category, but the presence of subcategories (and nodes within them) is ignored.
Any ideas?
Add header links to this code...
Just in case you want your parent headers to link to their own taxonomy pages:
swap this line:
Anybody know how I can easily page this output? Lots-o-tags and this page is huge.
Thanks.
Hey, Thanks very much for
Hey,
Thanks very much for this code -- I'm wondering how I should modify it to allow me to display users as opposed to nodes.
Cheers,
c;
DRUPAL 7
Hello
Someone tell me how to do this list but in Drupal 7
From already thank you very much.
some simple alternatives
The following post provides some code less alternatives:
http://drupal.org/node/128085#comment-3452522
Thank you very much! You
Thank you very much! You saved me a lot of time. :)