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:

 * 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

George Notaras avatar

About George Notaras

George Notaras is the editor of the G-Loaded Journal, a technical blog about Free and Open-Source Software. George, among other things, is an enthusiast self-taught GNU/Linux system administrator. He has created this web site to share the IT knowledge and experience he has gained over the years with other people. George primarily uses CentOS and Fedora. He has also developed some open-source software projects in his spare time.

9 responses on “Drupal Tip: List a node’s taxonomy terms inside a Block

  1. chanakya Permalink →

    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

  2. Administrare Damien Permalink →

    Many thanks. That will help me a lot!

  3. Sami Permalink →

    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.


  4. Ted Permalink →

    Thanks you have helped cheers for the code

  5. Jazza Permalink →

    Thanks alot for the code,

    How would I show the children taxonomy terms to the current node ?

  6. kay Permalink →

    Super useful, thank you !