Block view of nodes with same term id

Last modified: May 6, 2009 - 13:41

With the following sample, we get a block with nodes which have the same term id as the actual node:

<?php
if ($type=='block' && arg(0) == 'node' && is_numeric(arg(1))){
$node=node_load(arg(1));
$terms = taxonomy_node_get_terms_by_vocabulary($node->nid, 3, 'mykey');
foreach(
$terms as $term){
$city = $term->tid;
}
return
$city;
}
?>

Here 3 is the vid of the desired vocabulary, while 'mykey' can be any property of the $node object, is optional and if omitted will result in 'tid' (see api reference).

Actually the foreach() is less than desired, although necessary: it keeps overwriting the same value so that only the last item of the array is kept - it may be that the last one is also the only one, which is just perfect in my case.

 
 

Drupal is a registered trademark of Dries Buytaert.