Using the CCK and contemplate modules, I'm trying to create a custom template that displays a node as well as other nodes in the same category. The code below is working, but I'd like to be able to link to the related nodes' url_alias.

Is there an easier way to do this, or should I just query the alias while looping the related results?

$tid = NULL;
if (is_array($node->taxonomy) && count($node->taxonomy) > 0) {
	foreach($node->taxonomy as $key => $value) {
		if (is_object($value) && isset($value->tid)) {
			$tid =  $value->tid;
		}
	}
}
if (isset($tid)) {
	$sql = "SELECT p.* ";
	$sql .= "FROM content_type_product AS p ";
	$sql .= "INNER JOIN term_node AS t ON p.nid = t.nid ";
	$sql .= "WHERE t.tid = '{$tid}' AND p.nid != '{$node->nid}'";
}

Comments

mcjim’s picture

You should be able to do what you want with Views quite easily:
http://drupal.org/project/views
Docs here:
http://drupal.org/handbook/modules/views

geohelper’s picture

Thanks, I didn't realize you could embed a view into a node...
http://drupal.org/node/85769

mcjim’s picture

This is useful for excluding the current node from the view:
http://drupal.org/node/131547

patrickharris’s picture

drupal_get_path_alias('node/34');