Drupal Tip: List a node’s taxonomy terms inside a Block
It’s been several months since the last time I had done any coding on Drupal. Although many people might find it trivial, here is a PHP snippet to enter in a custom block, so that the taxonomy terms of the currently displayed node are printed as an unordered list inside that block. Also, each list item will be a link pointing to the relevant term page.
First of all, in order to be able to include PHP code, which will be evaluated, inside your posts, it is required to activate the PHP Filter module. Double check at admin/settings/filters that only trusted roles can use the PHP filter, otherwise your web site could be at risk. By default, only the administrator can use this filter.
To create the custom block, go to admin/build/block/add and enter a description and a title for your custom block. Paste the following code on the body textarea:
<?php /** * Prints an unordered list of the terms (as links) that are * associated to the currently displayed node. */ if ( arg(0) == 'node' && is_numeric(arg(1)) ) { $node = node_load(arg(1)); if (module_exists('taxonomy')) { $terms = taxonomy_link('taxonomy terms', $node); print theme('links', $terms, array('class' => 'node-terms')); } else { print 'No associated categories.'; } } ?> |
Make sure you select the PHP filter from the list of the input filters. Also, set the rest of the settings according to your needs and save the custom block.
Your new block should be available in the block list in admin/build/block and you should be able to use it straight away.
Now to some technical details about arg(0) and arg(1), which probably seem a bit cryptic to a user that is not experienced with Drupal (like me). Assume we have the following URL to a node: www.example.org/node/23, which means that the path to the page is /node/23. Well, arg(0) is the node part and arg(1) is the second part; 23 that is. Read about the arg() function.
This should explain the following part of the snippet above:
if ( arg(0) == 'node' && is_numeric(arg(1)) ) { $node = node_load(arg(1)); [...] } |
I give some emphasis on this as it is a snippet you will use quite often in order to use any of the node object’s properties inside a block.
The Drupal Tip: List a node’s taxonomy terms inside a Block by George Notaras, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
Tags: Customization, Drupal, PHP, Snippet, Taxonomy
May 7th, 2009 at 5:07 pm
Added to DrupalSightings.com
May 7th, 2009 at 6:54 pm
Jim, thanks. I appreciate it :)
May 8th, 2009 at 12:35 pm
Thanks
May 8th, 2009 at 12:36 pm
Thanks George. It was of great help to me.
Can you make small modification? I have 2 vocabulary – topics and author. I want only terms inside topics get listed. Is it possible?
Thanks a lot
May 8th, 2009 at 1:30 pm
This will be a bit more complicated, but possible, I guess. It will require to partially re-implement taxonomy_link() so that it uses taxonomy_node_get_terms_by_vocabulary() to get the terms from a particular vocabulary (topics).
May 15th, 2009 at 11:41 pm
Many thanks. That will help me a lot!
July 4th, 2009 at 2:35 pm
Thanks for the article. It helped me a lot. Can you please tell me how can I limit the number of tags (terms) to show in the block. For example I want to show only 5 terms in the block for the current node.
Thanks
July 6th, 2009 at 3:54 am
Thanks you have helped cheers for the code
July 13th, 2009 at 10:29 am
Thanks alot for the code,
How would I show the children taxonomy terms to the current node ?
July 30th, 2009 at 10:32 am
Super useful, thank you !
November 17th, 2010 at 10:19 pm
VERY THANKS!