Some Tips on Working with Drupal Taxonomy Terms
Today, while working on the redesign, and drupalfication of Cybertelecom, I was faced with a challenge: In addition to displaying the taxonomy terms that a node was filed under, I wanted to add a link to the term's RSS feed. The solution ended up being so bloody simple that I couldn't help but share it. Also, its worth pointing out that while this specific chunk of code is specialized, it reveals some damn useful tricks in dealing with the taxonomy from within single nodes.
In this tutorial's case, we will be calling the above function from within a node.tpl.php file. Within node.tpl.php file look for a line that reads:
<?php print $terms ?>Replace "$terms" with "$superterms". The reader should note that I came up with the name "$superterms" all by myself. Now, let us code the output of our quite cleverly named $superterms variable.
The Password is: taxonomy_node_get_terms($node->nid)
First, we need to get all information about the terms within our node. If we were stupid, we'd write a sql query. However, we are smarty pants, so we're going to do this the right way by calling the function taxonomy_node_get_terms($node->nid).
<?php $categories = taxonomy_node_get_terms($node->nid); ?>Now, if we were to print $categories we'd merely have the word "array" returned. You'll often run into this when programming for drupal. Luckily, you have a magic key: print_r(array_values($categories)). What does this do? Well, you can take a look below where I wrote it as executable php code.
Now -- I don't know why, but everytime a I see an std Object in an array, I know that I'll probably be using the php command foreach(). For whatever magic reason, PHP understands the following chunk of code to mean "for each std class object, execute the following code". Below is the entire chunk of code that you need to add at the very beginning of your node.tpl.php file
<?php $a = arg(1); $categories = taxonomy_node_get_terms($a); foreach ($categories as $category) { $superterms .= 'tid . '">' . $category->name . ''; $superterms .= 'tid . '/0/feed"> | '; } ?>Some explanations for the sick bastards who want to learn drupal programming
Now, for those you who are new to PHP, I'd like to explain a couple weirdnesses in this code that aren't really explained anywhere -- yet every developer uses these techniques (its stuff like this that makes learning PHP a nightmare for the beginner).
Most likely, you understand and remember from where the variable $categories came*. Clearly, it's holding the results of the taxonomy_node_get_terms($node->nid) function. However, as soon as we hit the foreach() command you'll notice that I wrote "foreach ($categories as $category)." Understand that "$category" is merely the singular of the plural "$categories". I could have written ($categories as $fromNicksAss) and, provided below i wrote $fromNicksAss->tid, it would have worked.
What's with those periods?
You'll note the bizarre use of periods within the foreach loop. These periods seperate functions and variables from static strings. In addition, they make life easy for those of us who have to write lots of themable functions.
So, notice that at the beginning of this tutorial I asked you replaced "print $terms" with "print $superterms". Now look at where $superterms is defined. That's right... the $superterms with the ".=" as opposed to a lonely "=" mean let's php know to expect additional code in the definition of the variable. The other periods merely seperate out variables that I want to execute. Play with it. you'll get it. Have to fly to Sante Fe tomorrow, so I'm out.
Notes:
*We are delighted that you took notice of our use of the queen's english.