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?

Comments

coreyp_1’s picture

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 tid of "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

ressa’s picture

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.

coreyp_1’s picture

Are you using the same block/view for both pages?

- Corey

ressa’s picture

yes

coreyp_1’s picture

try something like this:

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));
  }
}
if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))){
  return arg(2);
}

- Corey

ressa’s picture

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;
Anonymous’s picture

I am using this code:

$node = node_load(arg(1));
if($node) {
foreach($node->taxonomy as $term) { $terms[] = $term->tid; }
return implode ('+', $terms);
} else { return; }

ressa’s picture

Thanks for the code, the question above was for Drupal 5, so you only need the 'Taxonomy: Term ID' now. I am using this for a newer project in Drupal 6, to accomplish the same:

if (arg(0) == 'node' && is_numeric(arg(1))) {
  $node=node_load(arg(1));
  return implode('+', array_keys($node->taxonomy));
break;
} else {
  return FALSE;
}