Adding CCK node references to the taxonomy terms

Last modified: August 23, 2009 - 21:12

Description

A CCK node reference field lets you mark your node as having some other related nodes. You might want these other node links to show up among your taxonomy links, since these are also links of related content.

Step 1 of 2

Use this snippet in node.tpl.php.

Find the existing portion that prints out $taxonomy. It will look something like this:

    <?php if ($taxonomy): ?>
      <div class="terms"><?php print $terms ?></div>
    <?php endif;?>

Step 2 of 2

Replace it with the following, inserting the machine name of your field where required.

Uncomment the section of code that applies to you depending on whether you CCK field is set to hidden or not. The hidden version will work in all cases, but involves an extra node_load() call, so if the field is visible the visible version is quicker as it uses values already loaded.

<?php if ($taxonomy || $node->field_MYFIELD): ?>
  <div class="terms"><?php
   
print $terms;
    if (
$node->field_MYFIELD) {
      foreach (
$node->field_MYFIELD as $key => $item) {
       
# two choices:
        # if the noderef field is set to visible:
        /*
        $extralinks["item$key"] = array(
          # two choices:
          # if the noderef field is set to visible:
          'title' => $item['view'],
          'html' => TRUE,
        );
        */
        # if set to hidden:
       
$referenced_node = node_load($item['nid']);
       
$extralinks["item$key"] = array(

         
'title' => $referenced_node->title,
         
'href' => 'node/'. $item['nid'],
        );
       
# end
     
}
      print
theme('links', $extralinks, array('class' => 'links inline'));
    }
   
?>

  </div>
<?php endif;?>

Notes

You can also add node reference links to regular node links (alongside 'Read more', etc), using the same approach. Alternatively, you can use hook_link() in a module; but this won't work cleanly for adding to taxonomy links.

 
 

Drupal is a registered trademark of Dries Buytaert.