Drupal Taxonomy Tutorial

Tags:

One of Drupal's best features is taxonomy. This tutorial gives an introduction on how to use Drupal's taxonomy system.

New users to Drupal sometimes find taxonomy difficult to understand at first. I recommend reading all the way through this tutorial once. If the first part doesn't make sense, I hope that it will by the end.

Taxonomy can be thought of as categories or tags. Drupal taxonomy is made up of vocabularies and terms. A vocabulary is a set of terms and terms are just another word for categories.

The image below is from the taxonomy (categories) page located at http://example.com/admin/content/taxonomy:

Drupal taxonomy settings

Let's build the taxonomy for a news site as an example. First click on the Add vocabulary tab. You will then see a screen that looks something like the following image:

Drupal taxonomy vocabulary 1

For Vocabulary name type in News. For Description enter Categories that fall in the news section of the site. For Help text, enter Please choose a section of the Web site for this news story.

Continuing down the page, you will see something like the following image. It will probably have fewer options than what you see below. The example below is from a site that has many different content types. If you are using a new Drupal installation, you might just see two content type options: Page and Story. For this tutorial, just check the box for Story and leave the rest unchecked. That means that only Story nodes can use categories from the News vocabulary (remember that a vocabulary is a collection of terms or categories).

Drupal taxonomy vocabulary 2

The next part of the page has additional options:

Drupal taxonomy vocabulary 3

Check the option Single hierarchy. That means that categories will be able to have a parent-child relationship, with only one parent for each child term. I'll explain that concept below.

Check the option Required. That means that every story node (news page) will have to be placed in one category of the site.

Leave the other options unchecked for now. Here is a quick explanation in case you would like to use them on your site:

Hierarchy: Disabled
This means that categories do not have a parent-child relationship (sub-categories). All categories are on the same level.
Hierarchy: Single
Single Hierarchy means that your categories can have parent-child relationships. Each child can only have one parent category. For example, you might have a category called "Widgets", and then several subcategories called "Green Widgets", "Blue Widgets", and "Red Widgets". The subcategories could only be children of the "Widgets" category, and not subcategories of any other category.
Hierarchy: Multiple
Multiple Hierarchy means that your categories can have parent-child relationships and that each child can have multiple parent categories. For example, if you have two categories called "Cheap Widgets" and "Expensive Widgets", your subcategories, "Green Widgets", "Blue Widgets", and "Red Widgets", might be subcategories of both "Cheap Widgets" and "Expensive Widgets".
Related Terms
You can use this to choose related categories for each term, but I don't know if it has any use in Drupal's default setup unless you are writing custom PHP code.
Free Tagging
Free tagging allows you to enter categories on the node creation pages instead of choosing them from a list. Drupal uses AJAX to suggest possible categories that already exist in the database. This option is useful when you want to give your users freedom to define their own categories—for example adding tags to blog posts.
Multiple Select
This option allows you to select more than one category for a node.
Required
This option makes it so that you can't create a node without choosing a category for it.
Weight
The Weight controls the order that vocabularies (sets of categories) are displayed in.

After you have submitted the form as explained above, you will see something like the following screen:

New vocabulary added

The vocabulary has been created, but there are no terms categories in it yet. Click on the Add terms link.

Add a new term

For Term name enter Local. For Description enter Local News. Then click the submit button. You will be directed back to the same form where you can add another category. For Term name enter World. For Description enter World News.

After you have created the first two terms, create the third, but this time choose a parent category Local from the drop-down select box as shown below:

Choose a parent term

Name the term Sports. That will be the category for the local sports section.

Continue to add terms, in a hierarchical structure as shown in the outline below:

  • News [this is the name of your vocabulary]
    • Local
      • Business
      • Sports
      • Weather
    • World
      • Entertainment
      • Sports
      • Technology

It will look something like the following image:

Taxonomy hierarchy in Drupal

Pathauto Module and Taxonomy

For optimal results, I recommend also setting up the Pathauto Module. Using the Pathauto Module with Taxonomy will allow you to automatically generate custom URLs that put each news item in a different section of the Web site.

After you have the Pathauto Module installed, enter the following setting for Story Paths:

Drupal's Pathauto and Taxonomy combined

The settings [vocab]/[catpath]/00[nid]-[title] will create news URLs that look like http://example.com/news/local/weather/0025-page-title and were chosen for the following reasons:

[vocab]
This will be replaced with the name of your vocabulary— in this case news.
[catpath]
This will be replaced with the category and all of the parent categories. For example, a post in Local—>Weather will have a catpath of local/weather.
00[nid]-[title]
This might be a little bit controversial, but there is a reason for doing it. If you would like to someday be included in Google News you need a unique three-digit number in your news URLs. [nid] is the node ID which is unique for each node. Padding it with two leading zeros guarantees that the number will always be at least three digits. The [title] is the title of your post and it optional for people who want to include keywords in their URL.

Now you have setup Drupal to have a news section that will automatically organize your content by category and generate nice URLs for you.

If you also want blogs on your site you could create a second vocabulary called Tags and choose the freetagging option. Also choose Disable Hierarchy. Make the vocabulary required by checking the relevant checkbox on the Add Vocabulary page. Associate the Tags vocabulary only with the blog post content type. Choose a URL structure in Pathauto—perhaps something like blogs/[user]/[cat]/[title] or blogs/[cat]/[title].

If you have questions, please leave a comment below. See also the Pathauto Tutorial for more information about how to make Taxonomy and Pathauto work together.

Further Reading

For more about Drupal Taxonomy, see the Drupal Taxonomy Handbook.

If you are interested in actually building a news site in Drupal, check out the writeup of the New York Observer project.

Average: 4.1 (63 votes)

Comments

Thanks for the great

Thanks for the great article.

When following the link that is given as a motivation for having the three-digit number in the URL for Google News, it struck me that the article was rather old (2006). I figured, surely Google will by now have realised that unique URLs (one of the other requirements) are, in fact... unique, so that the addition of an extra identifier is not really useful.

Anyway, turns out that I was wrong, you still need it, Google themselves say so.
So why am I commenting, if the info is correct? :) You might want to consider changing the link explaining about the digits to this one, because that way it comes straight from the horse's mouth, as they say. I personally would not have gone on a search to verify the information if I came straight to that page.

Again, thanks for the article, it reassured me in what I thought I understood about taxonomy. Real world examples (and screenshots!) are really useful to get your head around these things.

Once you have the categories

Once you have the categories is there an easy way to generate a categories menu for Drupal like Wordpress does out of the box?

Webmaster Tips's picture

category menu for Drupal

There's the Taxonomy Menu module, but the last time I looked at it (which was a couple of years ago) it created duplicate content because it didn't link directly to taxonomy URLs, but created new URLs.

The project page says it integrates with the Pathauto module, so maybe that means the old URL problems were fixed.

Great article...thanks! You

Great article...thanks!

You don't actually need a 3 digit number in your URL if you submit a news sitemap to Google News. They don't really tell you that on Google until you get to the bottom of the page you linked in though!