Drupal: How to Show Nodes with the Same Tag
It is necessary to show titles of articles with the same tags as in current document in the block.
Solutions
- Use snippet
- Use Taxonomy Quick Find module
- Use Related Nodes
Use snippet
This is an approximate code which should be adjusted for a specified case:
<?php
//v.5
/*
Links are opened in a new window
If there is only 1 advertisement, 1 advertisement is shown (instead of the same advertisement shown 3 times)
*/
unset ($output);
$nmbr_adv=3;//Number of advertisement messages
if (arg(0) == ‘node’ && is_numeric(arg(1)) && ! arg(2) ) {
$node = node_load(arg(1));
$sql_cond=”;
foreach($node->taxonomy as $term_id=>$term_info) {
$sql_cond.= (($sql_cond) ? ‘ OR tn.tid =’.$term_id.’ ‘ : ‘ tn.tid =’.$term_id.’ ‘);
}
//eliminate duplicated nodes in the list
//check if the required number of advertising links are shown (if terms are specified)
$sql=”SELECT n.title, n.nid FROM {node} AS n INNER JOIN {term_node} AS tn ON n.nid=tn.nid WHERE n.status=1 “;
if ($sql_cond) $sql.=”AND (“.$sql_cond.”) “;
$sql.=”AND n.type=’adv’ GROUP BY n.nid LIMIT “.$nmbr_adv;
$result = db_query($sql);
while ($anode = db_fetch_object($result)) {
$output[] = l($related->title, ‘node/’.$related->nid);
}
return $output;
}
?>
Use Taxonomy Quick Find module
The module uses AJAX and provides interactive selection of articles by tags.
Use Related Nodes
Here is the official module’s page: http://drupal.org/project/related_nodes
This version is available for Drupal 5 only.