Drupal is a very powerful Content Management System (CMS) that has a well-designed taxonomy module. Taxonomy is the system of categorization and classification of things. In Drupal's taxonomy, you can define multiple vocabularies and the terms in those vocabularies can have multiple sub-terms. This site is set up that way with the high-level terms being general concepts such as "Programming" and "Technology." The one thing I wanted that it did not do out of the box was to show sub-categories as an additional navigation menu when you clicked on a parent category. It's not to hard to implement if you know how. Read on for the code snippets.
First off, you will need your own theme or you will need to modify an existing theme. That is beyond the scope of this article but should need help, you will find it at www.drupal.org.
This is for Drupal 6 but I believe with slight modification it will also work on Drupal 5.
This goes in your theme's template.php. It overrides the default taxonomy_term_page and adds in a set of links on the top that are the child terms of the parent. Since my vocabulary is called "Topics", I call the children "Sub-Topics."
IMPORTANT - change the word "themename" of themename_taxonomy_term_page to the name of your theme. My theme is called "rbgrn" so my function is called "rbgrn_taxonomy_term_page". If you're not sure what your theme name is, it will be the name of the directory or .info file. If your template.php has any override functions when you do this, they will also start with the name you want to use.
$output = '';
// Only display the description if we have a single term, to avoid clutter and confusion.
if (count($tids) == 1) {
$term = taxonomy_get_term($tids[0]);
$children = taxonomy_get_children($tids[0]);
$description = $term->description;
// Check that a description is set.
if (!empty($description) || sizeof($children) > 0) {
$output .= '<div class="taxonomy-details-wrapper">';
$is_wrapped = true;
} else {
$is_wrapped = false;
}
if (!empty($description)) {
$output .= '<div class="taxonomy-term-description">';
$output .= filter_xss_admin($description);
$output .= '</div>';
}
if (sizeof($children) > 0) {
$output .= '<div class="sub-topic-list"><h3>Sub-Topics:</h3> <ul class="sub-topics inline">';
foreach($children as $child) {
$output .= '<li>' . l($child->name, taxonomy_term_path($child)) . '</li>';
}
$output .= '</ul></div>';
}
if ($is_wrapped) {
$output .= '</div>';
}
}
$output .= taxonomy_render_nodes($result);
return $output;
}
Then in my style.css, I added this:
That creates the list of clickable sub-topics and styles it to make it look like it does on this site. It should work for any vocabulary.
9 Comments
Post a comment here or discuss this and other topics in the forumsI wish this module supports
I wish this module supports for creating category for various content types like page,story,video and image. But when I create the category for a video it isn't displaying the horizontal wise. I would like to review this topic on my native personal blog ทำบุญวันเกิด. Can you please tell me in what method it can be work? Thank you.
Check if child term has nodes
Great tip! I have been looking around for a simple solution like this, I edited it to not show "There are currently no posts in this category.":
Changed:
if (sizeof($children) > 0) {
$output .= '
';- ' . l($child->name, taxonomy_term_path($child)) . '
foreach($children as $child) {
$output .= '
';
}
$output .= '
';
}
if ($is_wrapped) {
$output .= '';
}
To this:
$output .= taxonomy_render_nodes($result);
if (sizeof($children) > 0) {
$output .= '
';- ' . l($child->name, taxonomy_term_path($child)) . '
foreach($children as $child) {
$output .= '
';
}
$output .= '
';
} else {
$output .= taxonomy_render_nodes($result);
}
What I did notice about this is that it lists all child terms regardless if the term has any content in it, how would I go about removing child terms that aren't used by any nodes and including a number next to the child term that indicates how many nodes are tagged by it (something similar to what "summary" does in views)
Any help is appreciated,
Thanks,
Michael
I suppose my question is:
I suppose my question is: Why would you have terms with no nodes in them? For future?
I don't know the drupal API well enough to say off the top of my head but what I'd do is look at the taxonomy lib and find a function that counts all nodes for a term and use that. If the count is 0, exclude from the list.
Business Directory confusion
Robert,
I wanted to hire someone to create a business directory using Drupal. Thus began my research on the Drupal website where I spent several hours reading other people's opinions about how to begin.
I saw a lot of conflicting information. First off, I noticed that a lot of people had made inquiries into the creation of a business directory, and that there were many threads among Drupal developers about working together on creating such a module. In other threads, many people warned against using Drupal altogether, suggesting instead Joomla or PHP MyBusinessDirectory.
Having only brief exposure to Drupal, I was quite confused. I had read extremely positive articles about the strength of Drupal's taxonomy module and was incredulous that Drupal could not easily handle the display of categories and sub-categories of pages.
One of the last articles to pull up in my quest for answers was this one. Looking at your website, it appears that you have accomplished what others have been clamoring for. So now I am even more confused.
Are other developers thinking beyond of what you have here and I didn't fully comprehend the scope of their plans? Or are others unaware of your work and believe that Drupal needs a full new module? Or is there something entirely different that I don't get?
To understand drupal is to understand the concept of a platform
Tom,
Drupal is just a platform. Technically you can do almost anything in drupal. It just depends on how much code you feel like writing. I like drupal because I feel that it's a well-designed platform that provides very solid core functionality for any site.
The sub-categories modification that I made was really easy and simple. I'm not sure why people were acting like it is impossible or hard. Drupal started as a platform for programmers but it's become so much more accessible over the years that the audience has switched to more regular users that don't possess programming ability. The problem with that is that they see things differently and regularly expect to be able to plug in a module for any specific task - which does work often but isn't always the case.
I'm going to have to say that for any really good drupal implementation - including implementing a custom theme, you will always want a good developer there to do the job.
Very few things, if any, are impossible.
Thanks a bunch, though it
Thanks a bunch, though it didn't work for me (Drupal 6). I got a blank page when viewing a sub-category and when viewing a parent get this error:
warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'themename_taxonomy_term_page' was given in /var/www/html/includes/theme.inc on line 617.
Theme Name?
I updated the code. It had my theme name, "rbgrn", in it but now is just "themename". Did you replace that with your theme name? If not, which theme are you using and what is your themename_taxonomy_term_page function called?
Drupal 5 or 6?
This seems#to be just what I'm looking for, but you didn't mention what version of Drupal it works with.
Thanks!
And the version is...
Drupal 6. I'll add that to the article.
Thanks!
Post new comment