Drupal: List child taxonomy terms of current parent term
Task
On a recent project a client asked for the following: when visiting a taxonomy term, if that term has children, to display a new block displaying the children terms. The block would also need to have the parent term as the title.
Step 1: Function
Inside your template.php file add the following:
<?php
function themename_child_terms($vid = 1) {
if(arg(0) == 'taxonomy' && arg(1) == 'term') {
$children = taxonomy_get_children(arg(2), $vid);
if(!$children) {
$custom_parent = taxonomy_get_parents(arg(2));
$parent_tree = array();
foreach ($custom_parent as $custom_child => $key) {
$parent_tree = taxonomy_get_tree($vid, $key->tid);
}
$children = $parent_tree;
}
$output = '<ul>';
foreach ($children as $term) {
$output .= '<li>';
$output .= l($term->name, 'taxonomy/term/' . $term->tid);
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
}
?>Step 2: New Block
Create a new block and add the following to the block body. (Be sure to select the input as PHP):
<?php // $vid is the vocabulary id.
print themename_child_terms($vid = 1);
?>Step 3: Alter Block Title
Create a block-block-blockid.tpl.php file inside the default theme directory. Put the following inside the new file:
<div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="block block-<?php print $block->module ?>">
<?php // let's get the parent term title for the child term we are visiting
$parent_title = array_reverse(taxonomy_get_parents_all(arg(2)));
if($parent_title) {
$block->subject = $parent_title{0}->name;
}
?>
<?php if ($block->subject): ?><h2><?php print $block->subject ?></h2><?php endif;?>
<div class="content">
<?php print $block->content; ?>
</div>
</div>Tip
The function could be added to the block body (block edit form) instead of the theme's template.php file. Calling the function inside the template.php file will create an extra step of security, preventing the admin/client from breaking the block (accidentally altering/removing the function) when editing.
Comments
Hi Elvis, I need to design
Hi Elvis, I need to design a custom frontpage that will do the same thing that you have explained in this post. Any ideas or tips? Thanks
Hi Elvis, do you also have a
Hi Elvis, do you also have a D6 solution for this please?
@Martijn: that code actually
@Martijn: that code actually works on Drupal 6.
Hi, I only get to see the
Hi,
I only get to see the childterm with a bullet in front of it.
So when taxonomy/term/1902, for my site is aliased to:
/india, then I only see the subterms like:
-
.Kerela
-
Greetings, Martijn
Thanks so much for this
Thanks so much for this code; dropped in, worked perfectly. Great work!
I don't know why this does
I don't know why this does not work in my Drupal 6.14 website.
In Drupal 6, you have to
In Drupal 6, you have to empty cache and theme registry when making changes to template.php. You can do this from:
admin/settings/performance
If that doesn't work, check watchdog and any PHP errors to get an idea of what is going on.
Great snippet! Is there
Great snippet! Is there anyway this could be modified to return a list of terms separated by +, for use in views arguments? (very usefull if one is using composite or panels nodes).
Thanks for this. Fits my
Thanks for this. Fits my needs almost perfectly.
But how do I get the block to continue to display when I'm showing one of the nodes belonging to the children? At the moment, the block just disappears if I'm not on a taxonomy page.
I'm assuming I'll need to pass the $nid to the function from somewhere and work out the $vid from there, but the first step is to work out why the block doesn't appear on a node page.
Hi James, if you look at the
Hi James, if you look at the first part of the code, it checks to see if the current page is /taxonomy/term/xx, or arg(0)/arg(1)/arg(2)
If arg(0) != 'taxonomy', then nothing happens. Try this.
I was very happy to find
I was very happy to find this code snippet but it is returning the term description instead of child terms (running drupal 6.15).
I changed "themename" to my theme's name (mytheme) and added the code to my template.php.
Then, I added the print mytheme_child_terms($vid = 5); code to the page header on a views taxonomy page. (The vocabulary id is 5)
When I visit a views taxonomy page, it breaks the formatting and simply returns the term's "description" as html.
What I am doing wrong? How can I add this function to a view?
Hi Milo, I have not tried
Hi Milo, I have not tried this on D6 yet. But, I am sure there is a module available for this functionality. When I made this there was nothing available for D5.
If you don't mind, report back if you find a D6 solution. If not, i will test the snippet on D6 and find the issue.
Cheers
Hello. Sorry for my poor
Hello. Sorry for my poor english. I am quite new to Drupal. I would like to ask, if is it safe to use $term->name and similar variables. Is there some htmlspecialchars PHP function applied on these variables? I have created a taxonomy term which contained html tags. After printing out this term with the function l($term->name, $url), the html tag was embedded into the html code. So I think this is not very safe... especially when visitors can tag nodes.
I tested the code on my
I tested the code on my yohog site, but the block is not displaying, there's no error either. Btw i'm using D6 and adaptive theme. I think I will go for CCK to do this.
Oh i just found a module
Oh i just found a module that work for D6, here it is http://drupal.org/project/taxonomyblocks
$output .= l($term->name,
$output .= l($term->name, taxonomy_term_path($term));$output .= l($term->name,
$output .= l($term->name, 'taxonomy/term/' . $term->tid);
Replase to
$output .= l($term->name, taxonomy_term_path($term);
This was super great! But
This was super great! But alas, the child terms won't translate to other languages. Any ideas?
It seems that no matter what
It seems that no matter what site I build, I always want taxonomy to behave this way. In Drupal 7 (and possibly 6?) I was able to do this easily for the term pages by using the default views replacement of the term pages and changing the argument: Taxonomy: Term ID (with depth) The depth is set to 0 by default. Just change it to a higher number and the child terms will show on parent term pages. I assume that 1 will only show children, 2 will show children and "grandchildren", etc. Does anyone know if there is a performance hit for choosing larger numbers? Why shouldn't I just choose 10 in case my hierarchy becomes more complex in the future?
Thanks for this one ! but
Thanks for this one ! but how to add taxonomy image attached to term ? By advance, thank you best regards
Add new comment