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.
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">
| ';
}
?>
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.
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.
*We are delighted that you took notice of our use of the queen's english.
Comments
thank you for sharing very
thank you for sharing very useful code!
I understand that this is from 2005, but taxonomy_node_get_terms is the same in the API in drupal 6 and 7
yet when one tries to use your snippet
, an empty array is returned, even when you try to use print_r(array_values($categories))
(the default theme and other themes)
This has been reported by others and mentioned on IRC:
http://drupal.org/node/44938
any ideas?
not exactly the same, you
not exactly the same, you pass a node to it, not, $node->tid, says the api.
Simple Filter by Taxonomy Name
Thanks for opening the taxonomy_node_get_terms function to me.
Any suggestions for looping over a particular taxonomy term (or terms). On my home page, i'd like to have a block that looped over the terms 'City News' and 'Press Releases' and only printed nodes that fit either of those two terms.
Great site!
l function
Probably should generate the link using the l function instead
http://api.drupal.org/api/4.7/function/l
Drupal Taxonomy link URL
Hello Nick and Daniel,
has anyone been able to resolve this url scheme for the taxonomy terms?
/taxonomy/term/xyz
I just configures the taxonomy on one of my site and everything looks good but this kind of URL looks ugly and I have not been able to find out a workaround for this.
I have just wrote a short step by step guide on how to Configure Categoryies/Vocabulary on Drupal 5.X, for anyone who is looking for that kind of approach. It is not as advanced as your topic but I believe will give out a basic idea to a newbie (like me).
Let me know if you have a solution for the link url problem for these labels or tags.
Thanks in advance,
Nitin
pathauto works for me
I use pathauto on my blog.
It's not really the most elegant of solutions, and my normalization conscience wimpers at having to explicitly declare an already exiting relation (term id to term name) again in the alias table.
But it does what you need, automatically and out of sight.
More detail is here:
http://arancaytar.ermarian.net/2007/03/23/news-technology-categorization
always a newb!
Thanks for this posting, oddly enough i read it before, but never understood till now, cause i needed to know! Thanks!
RE: I'M TORN ABOUT NAMING...
I must agree about the name. This looks like a rather interesting and useful tutorial for those who need to know this. However, the very first paragraph made it clear that this article was not what I was looking for: a compendium of clear and concise tips for formulating, implementing, maintaining and working with a Drupal taxonomy.
I am building and blog- and webzine-based community forum for writers, artists and musicians, and the taxonomy system seems to be a perfect way to organize the various themes, categories, keywords, etc. in my site. What I hope to find is an article offering some practical advice for developing a user-friendly (and admin-friendly!) taxonomy that is applicable to a variety of applications. I see how the info in this article could be useful...once I get to that stage...but for now I need more of a beginner's tutorial (and something a little more helpful than the Drupal manual!)
Just my two cents and some dollars worth of rambling...
PS.... FROM WHENCE the $categories variable came
not working on live server
/taxonomy/term/xyz URLs are
/taxonomy/term/xyz URLs are UGLY
nice tip!
Thanks for the great tip. I am using the methods described above to ascertain the current node's taxonomy Id so I don't have to repeatedly make a bunch of otherwise the same flexinode templates for wrapping views. Here, I'll show you:
Starting with bomarmonk's patch to views to allow views to become embedded into a flexinode and thereby become a card-carrying member of a book with
<?php print render_view_page(2); ?>, I then created a view filtering on file type(s) and added an argument to view only a given departments' taxonomy terms. Now, with your help, I'm able to dynamically determine that tax termId and pass it in to the bomarmonk's view-render function. Here's a basic flexinode template with embedded view:<div class="node<?php if ($sticky) { print " sticky"; } ?>"><?php if ($picture) {
print $picture;
}?>
<?php if ($main) { ?><h2 class="title"><a href="<?php print $node_url?>"><?php print $title?></a></h2><?php } ?>
<?php
$arrcategories = taxonomy_node_get_terms(arg(1));
foreach($arrcategories as $catterm) {
$cattermid = $catterm->tid;
break;
}
print render_view_page(2, $cattermid); ?>
<span class="taxonomy"><?php print $terms?></span>
<div class="content"><?php print $content?></div>
<div class="links">» <?php print $links?></div>
</div>
Notice I'm first determining $cattermid with your tips and adding that as the argument to the view. Why I had to do a foreach instead of something like the following I don't know:
$cattermid = $arrcategories[0]->tid;I'm torn about naming so
This is excellent!
Post new comment