6

On the node page I'd like to add a linked list of other nodes tagged with the same tag/taxonomy term. How can this be achieved? Via Views?

Any help will be highly appreciated

4

There's a module that does that for you: Similar by Terms. But you can also do this with Views using Contextual Filters & Relationships. This comment on Drupal.org explains it quite well:

Its really easy in D7. Coding is not at all required.

Taxonomy is a field in D7. I will explain how to create a Related Content block using Views.

Create an argument by adding the taxonomy field on which you want to find the related nodes. Select "Provide default argument". Argument type = "Taxonomy Term ID from URL" Remove tick from "Load default argument from term page" Tick the "Load default argument from node page, thats good for related taxonomy blocks." To remove duplicates you have to follow the following points.

Open "Query settings" in the "Advanced Settings". Tick "Distinct". This gives you a perfect "Related Articles" or "Related Nodes" using Views.

  • Thanks Baysaa. I'm actually trying to do this programmatically. hmmm... – Daniel Sachs Jun 22 '12 at 22:59
  • @DanielSachs then you can just copy the code you need from that module ^^. Or use it as a guide. I'm sure it'll be better as it should've gone through the issue logs, bug fixes, security reviews etc. No point in re-inventing the wheel I always say. That's what Open Source is about. – Beebee Mar 1 '13 at 16:46
2

Ok, a bit frustrating due to lack of documentation and me being relatively new to Drupal. So here we go:

#Let's find out what term is used
$my_node_tag = $node->field_your_field_used_in_node['und'][0]['tid'];

#Get the array of nodes IDs
$nodeid = taxonomy_select_nodes($my_node_tag, FALSE);  

#And here we go
$counter = 0;
foreach($nodeid as $nid) {
    $node = node_load($nid);
    $alias = drupal_get_path_alias("node/$nid");
    print '<div class="title"><a href="' . base_path() . $alias.'">';
    print $node->title;
    print '</a></div>';
    $counter++;
}

Lovely. Not sure how efficient this is, but it works.

0

Not really needed to install other module when we can achieve this using core ones see this tuto to fond out how.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.