List related nodes in both node and term view using Views
ressa - July 22, 2008 - 22:17
Based on the code in this thread, I managed show related nodes in a block when looking at a single node:
Views Argument:
Argument: Taxonomy: Term ID - Display All Values
Argument Handling Code:
//return args key 0 as + separated string of tids
if(arg(0) == 'node' && is_numeric(arg(1))){
$nid = arg(1); //node id
$tids = db_fetch_array(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $nid)); //simplest query for node tids
if($tids){
return array(implode('+', $tids));
}
}But what if I want to also show related nodes when looking at a term?
For example:
I have a Vocabulary called Country with the following terms:
Country
-Denmark
-Sweden
-Germany
When looking at a single node tagged with the term Sweden, only Swedish nodes are shown, but if I click on the term Sweden, nodes with the other countries are also shown in the "related nodes" box.
Does anyone know how to accomplish this?

I'm not exactly sure what
I'm not exactly sure what you are trying to accomplish.
When "Sweden" is clicked on, it should take you to a page like this: "taxonomy/term/#", where # is the
tidof "Sweden". On this page, Drupal will automatically give you a list of every node that is tagged with the term "Sweden".Is it *not* happening like this? If so, then what do you want to happen differently? If not, then what is actually happening when you click on "Sweden"?
- Corey
It is happening like you
It is happening like you describe in the content area, when clicking on "Sweden": Drupal displays a list of every node that is tagged with the term "Sweden".
I will try to clarify:
I have made a view which is being shown in a block in the right sidebar. It displays nodes tagged with the same term as the current node, for example "Sweden", when looking at a node.
What I want is to have nodes tagged with "Sweden" shown in that block when clicking on that term("taxonomy/term/#"). As it is now, nodes tagged with all terms ("Denmark", "Sweden" and "Germany") are being shown in the block.
Thanks.
Are you using the same
Are you using the same block/view for both pages?
- Corey
yes
yes
try something like
try something like this:
<?phpif(arg(0) == 'node' && is_numeric(arg(1))){
$nid = arg(1); //node id
$tids = db_fetch_array(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $nid)); //simplest query for node tids
if($tids){
return array(implode('+', $tids));
}
}
if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))){
return arg(2);
}
?>
- Corey
I found a solution where I had already looked...
Hi Corey,
Thanks for your suggestion which didn't work, though I can't really see why... I appreciate your effort!
I went back to the thread where I found the original code I started out with and there was actually a solution to show related nodes, viewing either a node or taxonomy view:
1. Add two arguments, the first one is NODE:Node_id with 'not equal' option and the second one is TAXONOMY:taxonomy_term_id (Display All Values for both).
2. Add the following argument handling code:
$retArgs = array();
if(arg(0) == 'node' && is_numeric(arg(1) ) ) {
$nid = arg(1);
$retArgs[] = $nid;
$tids = array();
foreach(taxonomy_node_get_terms($nid) as $term) {
$tids[] = $term->tid;
}
if ($tids) {
$tids = implode('+', $tids);
}
$retArgs[] = $tids;
} elseif (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
$retArgs[] = 0;
$term = (taxonomy_get_term(arg(2)));
if ($term) {
$retArgs[] = $term->tid;
}
}
return $retArgs;