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.
Drupal Tip: List a node’s taxonomy terms inside a Block by George Notaras is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2009 - Some Rights Reserved
Jim, thanks. I appreciate it :)
Thanks
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
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).
Many thanks. That will help me a lot!
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
Thanks you have helped cheers for the code
Thanks alot for the code,
How would I show the children taxonomy terms to the current node ?
Super useful, thank you !