Drupal Groups (OG) - List Groups on Nodes

It took a few hours to find but after digging through some foreign web sites I finally found the code that I needed to add the parent group names to a node.

I needed to list the group(s) that a node belonged to within a Drupal node template, (node.tpl.php), and it was nearly impossible to find the code I needed. Most of my sanity is out of the window. I had to install Content Templates to get the variables I needed to figure out how to display the group names of the Groups that my nodes belonged to. I tell you what. Then I searched for those variables and came to some site that wasn't an english site, but thank goodness for english code. And here is the magical code.

				
	$group_links = array();
	foreach ($node->og_groups_both as $key => $name) {
	$group_links[] = l($name,'node/'.$key,array('class'=>'group'));
					}
	$group_links = implode(', ',$group_links);
					
	print ($group_links);
				

Of course add your opening and closing php tags to that code and voila. This will add any groups that the node belongs to and separate multiple groups if it belongs to multiple groups with a comma. It also links to those groups. It's some awesome code. Let me know if you run into any issues. This works on Drupal 5.7 and Drupal 5.8 for me.

No votes yet