How to get taxonomy terms for the current node from a block
chandika - July 31, 2006 - 12:50
Hi All,
I'm trying to build a block that shows the related nodes to the current node by taxonomy term. For example: If the current node in display has a taxonomy term "fish", a bunch of links that link to nodes that have the "fish" term is to be shown in a block on the page.
I have looked all over the forums and the handbook, and I did not see this addressed before.
I have worked out how to get the related nodes by category using the book page at http://drupal.org/node/2498 . But my question is how do I get the current nodes taxonomy terms from that Block?
Any ideas?

Load the terms via the node id
<?phpif (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
$terms = taxonomy_node_get_terms(arg(1));
}
?>
Works!
Thanks nedjo. Now the block works great. It displays top 5 related nodes by the taxonomy term. The code is below if someone else is looking for something similar.
<?php
if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
$terms = taxonomy_node_get_terms(arg(1));
$output .= "<ul>";
foreach($terms as $term){
//$taxo_id = 7;
$sql = "SELECT node.title, node.nid FROM node INNER JOIN term_node ON node.nid = term_node.nid WHERE term_node.tid = $term->tid LIMIT 5";
$result = db_query($sql);
while ($anode = db_fetch_object($result)) {
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
}
$output .= "</ul>";
return $output;
}
?>
adapted as a PHP snippet:
adapted as a PHP snippet: http://drupal.org/node/76923
Specific vocabulary links
This is great but is there a way to display terms from only one specific vocabulary, I have one node related to three vocabularies but want to display links just from one of them?
Did you get a solution for
Did you get a solution for the above ??
I am exactly looking for the same.
Hi, Is there anyway I put
Hi,
Is there anyway I put this code just below the node's content? I don't want to it's as a block, and making it part of a footer just seems silly. I tried pasting this code in page template and node template but none of it works.
solution when you don't use node for URL?
Is there a solution for situations where you don't have URLs like "node/10". Speciflcally I use pathauto for all my nodes - I'm trying to think of a way to adapt this for the situation where the url is something like
"category/title-of-node"
Any ideas?
Greg
--
Growing Venture Solutions
Drupal Implementation and Support in Denver, CO
Use of arg(n) reflects unaliased name
Use of arg(n) reflects unaliased name so the use of arg(0), arg(1), etc should even work if paths are aliased.
Is this
Is this possible in the Views module? I've never quite understood how to do something like this in Views, so if you guys have any suggestions, I would appreciate it. Thanks.
try to use the $n object
In these cases, the $n object is very handy...
what do you get from this?
echo "[".$node->nid ."]";I've used the function above for a "very standard" translation at http://roma.cercachetrovi.it/ using a taxonomy vocabulaty "Language"
nevets comment solved it
nevets comment two above this one solved my problem. I should have posted back - my bad.
--
Knaddison Family | mmm Free Range Burritos
I found a nice way too, and
I found a nice way too, and that is to implement a hook_nodeapi function, intercepting a 'load' with a node of matching 'type' (or other condition), and then sets a global variable describing the node.
The block can simply check if the global variable is set, and can get whatever data it likes from the node through the variable.
Advantage here is that you don't need to read the url - which is nice when the url path is mapped by another module.
The only caveat I see is that it relies on drupal loading the node before building the blocks. This is how it works in Drupal 6.x.