Show related or similar content in a block with Views 2 in Drupal. Here is a step by step tutorial on how to accomplish this.

I first found this method over at Drupal Easy: Using Views 2 and Drupal 6 to Create a Related Pages Block. I'm going to show you a similar method with a few minor adjustments.

As of this writing this is accomplished with Views 2 and Drupal 6.

  • Make a new view and call it whatever you want.
  • Add a Block Display
  • Add a new argument - Taxonomy: Term ID
    • Set up the argument with these options:
    • Provide Default Argument
    • Php Code:
    • $node=node_load(arg(1));
      if($node){
      foreach($node->taxonomy as $term){$terms[]=$term->tid;}
      return implode('+',$terms);
      }else {return;}
    • Check Allow Multiple Terms per Argument
    • Check Reduce Duplicates
  • Click Save

You need to Define at least one Field. I recommend adding Node Title.
I would also recommend adding some sort of sorting. You can have it by Post Date or if you have Statistics enabled in your Core (optional) modules, you can sort by what content is the most popular.

Now here is the added bonus. It doesn't make much sense to display the same piece of content that you are currently looking at. So here is how you remedy that.

  • Add another Argument - Node: Nid
  • Then set the argument with these options:
    • Provide Default Argument
    • Node ID from Url
    • Check the Exclude from Argument
  • Click Save

This takes out the current piece of content that you are currently viewing. Simply drop this block in a sidebar and Views will take care of the rest.

Make sure and Subscribe to this blog to follow along with other Tips, Tricks, and Tutorials.

Comments

Fantastic!!

It was a great help to me. However, I would be obliged if you can solve a query of mine.
I want to create a view that takes the taxonomy term of current node and searches for other nodes that share same taxonomy term. The other nodes are of different content type with different vocabulary but same term name.
Furthermore, I have aliased my urls.
Please provide me some guidance to achieve this.
Thanks!

it allows the user to

it allows the user to reference a node that doesn't exists. In this case it creates a new node with the body text taken from the first matching Wikipedia entry. The idea is that this is just seed content, and the user will eventually update the content for themselves

Tried it. It simply doesn't

Tried it. It simply doesn't work at all.

Excellent!!

It works 100% for me.
Simple and elegant.
Many thanks for providing!!

abramo

Thank's

Simple "The Best"

Good job

Very simple and step by step....Thanks.....

is it possible to do this on

is it possible to do this on a taxonomy page rather than a node?

Should be Possible

If you are talking about the default Taxonomy page that Drupal spits out then no. If you are running your taxonomy through Views then I don't see why it wouldn't work.

Works great

Well done! I'm just beginning to understand how arguments work in views -- understanding how this works will help me along.

Thank you for taking the time to post this and to lay out the steps so clearly.

Slight bug

Your argument code needs to make sure the $node->taxonomy array exists before it tries to implode it. I altered your PHP to this:

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

Hide opened node

Its simple solutions, but I have a question.

How to hide the opened node?

Look into the text again:

Look into the text again: solution comes after words 'Now here is the added bonus...'.

Thank You Szy. It work well.

Thank You Szy.

It work well.

Absolutely marvelous

I took this, used it to build a Node Content View then by using Panels 3 I created Festival pages that incorporated all the blogs and YouTube vids from the acts.

Great but...

http://www.likal.com/uk/cheltenham/content/zenith

At the moment I use it here and it returns nodes which have terms in "local bands" taxonomy.

What would be ideal is "returns nodes where the terms are in THIS NODE", then a taxo filter by vocab - so only "local bands" show - would in effect provide "other nodes about THIS BAND".

I'd be happy to bounty this, donate to charity or write it up for drupal org as loads of people would like it.

FIY my site puts view into the page by using the lovely panels3.

Regards

Ian

Perfect

... just what I've been looking for. I never knew the power of Arguments in views. This, and the edited code by by Anonymous on Wed, 06/10/2009 - 06:56 makes this perfect. Thanks!

thanks

... that helped. Such perfectly targeted snippets always are a problem with Drupal and especially its documentation. Perfect and straight to the point. Thanks!

Sort by number of common terms

Do you have an idea, how to sort it by number of common terms?

... then your solution would be complete.

Anybody, please? :]

Anybody, please? :]

Cool

Thanks, i was looking for something like this thanks

uncategorized nodes produces

uncategorized nodes produces an error.

i see. sorry i didn't notice.

i see. sorry i didn't notice. this code fixed it.

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

Thanks

Ah, i noticed a error once or twice but this did the trick for me!

Works Perfectly!

Thanks for your snippets - I am going to keep this page bookmarked for future use!

Great

Very helpful -- I had searched for hours. Thanks!!

Content Taxonomy

Hello,
i have got two Content Taxonomy fields and i want to create related content with views.
how do i have to alter the code to reach this aim?

Benny

It doesn't work correctly

this solution showing related content, but showing also in the block already opened content. It should work like module "Similar By Terms", but doesn't :(

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options