Displaying similar nodes
Question
I've somewhat ran in to a problem with Drupal today.
I would like to display a node (Product) on a page, and below that node, I'd like to display 3 similar nodes (Products). Similar being: having the same taxonomy id or having a "promoted" tag attached to it.
I've tried crafting the related nodes into a view which is being displayed as a block, only when we're on a product's page. I didn't get far with this.
My second thought was making a panel page with 2 views on it, one for the product, and one for the related products. I also didn't get far with this.
Does anybody know the easiest way to accomplish this?
Update:
I have tried both answers, I am not receiving any related products though. The SQL query that's executed (term id = 1) is:
SELECT node.type AS node_type, node.title AS node_title, node.nid AS nid, node.created AS node_created FROM {node} node INNER JOIN {taxonomy_index} taxonomy_index_value_0 ON node.nid = taxonomy_index_value_0.nid AND taxonomy_index_value_0.tid = :views_join_condition_0 WHERE (( (node.type IN ('product')) AND (taxonomy_index_value_0.tid AND '') AND( (taxonomy_index_value_0.tid IN ('1')) ))) ORDER BY node_created DESC LIMIT 10 OFFSET 0
When I manually execute the query and remove AND (taxonomy_index_value_0.tid AND '')
from the query I do receive the related products.
Does anybody know what causes the code to be added to the query and how to fix it?
Update 2: I've removed the "Allow Multiple Terms per Argument" and am now getting the related products. I don't know what this means for my site though.
Update 3: I am using Drupal 7 by the way.
Solution
Override your node view with panels. And create a view block with 'taxonomy id argument', you need to choose default argument options as PHP Code and place this code.
$node = node_load(arg(1));
if($node) {
foreach($node->taxonomy as $term) {
$term = $term->tid;
return $term;
}
}
I just launched a site using panels + views magic. http://sgigulf.org/culture/synopsis-of-performers-showcased-by-sgi-gulf
OTHER TIPS
Take a look at the RelatedContent module. Links to the module and a couple of tutorials below:
- http://drupal.org/project/relatedcontent
- http://drupaleasy.com/blogs/ryanprice/2008/06/using-views-2-drupal-6-create-a-related-pages-block
- http://www.hankpalan.com/blog/drupal/related-content-views-2-drupal
You say you're having trouble with the display. In that casea, make the view from the above instructions a block, and have it display in a region that's below the node content, though that assumes there's a region in your theme directly below your content.
You can have multiple terms when you change to
$node = node_load(arg(1));
if ($node) {
$ret = array();
foreach ($node->taxonomy as $term) {
$ret[] = $term->tid;
}
return implode('+', $ret);
}
return '';
The '+' in implode is OR. If you want AND, than use ',' instead