Tech4Him – Technology with Integrity

A Christian technology chaos wrangler and his thoughts


Yay! Related Terms with Views 2 and Drupal 6. Hooray!I was working on a Drupal site and decided that a “related content” block was needed. Since this site has already been up and running for some time, manually reference other nodes would be painstaking. What I needed was the ability to show related node based upon the content of the node.

At first I began thinking about something using the Drupal search collections to perform a search and then show node results. This might be nice on a more commercial and large content site. In this case, this is a niche site and they are already doing a good job of tagging content with terms.

Thanks to a quick Google search, I found an excellent article over at DrupalEasy for creating a related nodes by term block using Views 2. (Yay! one of my favorite modules.)

For those who don’t want to type the argument code, here it is. This loads the current node object and create a string of terms. This is used to find other nodes with the same terms.


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

Now in my case, I also did not want the currently viewed node to show up in the list. (That would be weird.) So, add another argument for Node: nid. Set the option to provide a default argument if argument is not present. Then set the default argument type to Node ID from URL and finally check the box to exclude the argument.

That check box can be misread. It is not saying that it is going to exclude the argument such that the argument has no bearing on the query. Instead it means that it will use the argument to exclude results with this argument value. (Just a little something with the words used.)

Really, the only other changes I made was to add the node type to the fields displayed and I wanted to be sure we didn’t always see the same results for each node. So, I added a “Global: Random” sort criteria to the view. This way I get a random set of nodes to display as related content. Keeps things a little more fresh that way.

13 minutes later, my block is showing related content by term.

Now, if you only want to consider a particular vocabulary, the PHP argument code is different.


$node = node_load(arg(1));
if ($node) {
$terms = taxonomy_node_get_terms_by_vocabulary($node, 1);
return $terms[1]->tid;
}
return false;

Blessings.

Popularity: 15% [?]

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • LinkedIn
  • PDF
  • RSS
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter

One Response to “Creating a Related Pages Block with Views 2 and Drupal 6”

  1. Kristen says:

    Here is a views export using views and the views group by module to show the block along with the number of common terms:

    http://kristen.org/content/use-drupal-views-group-module-show-closest-matches-terms

    Also, for Drupal 6, you can check out the featured content module for easily creating lots of different kinds of featured and related content blocks including sorting by closest match by terms:

    http://drupal.org/project/featured_content

    Cheers,
    Kristen

Leave a Reply