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.

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-ShareAlike 4.0 International License.
Copyright © 2009 - Some Rights Reserved

About George Notaras

George Notaras is the editor of G-Loaded Journal, a technical blog about Free and Open-Source Software. George is a GNU/Linux enthusiast, a self-taught programmer and system administrator. He strongly believes that "knowledge is power" and 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 and spends some of his spare time developing open-source software. Follow George on Twitter: @gnotaras

11 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. 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.

    Thanks

  3. Jazza Permalink →

    Thanks alot for the code,

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>