I have a view sorted by terms in a (taxonomy) vocabulary. Views took care of the sorting for me. I wanted headers to be displayed each time the term changed. For example, imagine I had:

  • 15 story nodes about sports
  • Vocabulary 'Sports' with terms 'baseball', 'football' and 'hockey'
  • Each story is tagged exactly once

I wanted my output like:
Football
NY Giants Win Superbowl
Washington Redskins Looking Strong
Hockey
Ovechkin, the next great one?
etc.

I spent a lot of time reading forums and googling and didn't spot a way to do this. Here is how I accomplished it:

<?php
//from my Row style output: views-view-fields--my_viewname.tpl.php
if( !isset( $GLOBALS['my_very_unique_index'] ) ) { //unique so it does not overlap w anything else
	$GLOBALS['my_very_unique_index'] = false;
}

if( $GLOBALS['my_very_unique_index'] != $fields['tid']->content ) { // sorting on Tax, added tax term to the fields
	$GLOBALS['my_very_unique_index'] = $fields['tid']->content;
	print '	<h2>'.$fields['tid']->content.'</h2>';
}
?>

The actual file contents are:

<?php
	if( !isset( $GLOBALS['current_te_in_the_news_tag'] ) ) {
		$GLOBALS['current_te_in_the_news_tag'] = false;
	}
	
	if( $GLOBALS['current_te_in_the_news_tag'] != $fields['tid']->content ) {
		$GLOBALS['current_te_in_the_news_tag'] = $fields['tid']->content;
		print '	<h2>'.$fields['tid']->content.'</h2>';
	}
?>
<a href="<?php print $fields['field_url_value']->content; ?>">
	<?php print $fields['title']->content; ?>
</a>
 - <?php print $fields['field_publication_date_value']->content; ?>
 <br />
<?php
	switch( $fields['field_publication_type_value']->raw ) {
		case 'print':
			 print '<i>'.$fields['field_publication_nid']->content.'</i>';
			 break;

		default:
			print $fields['field_publication_nid']->content;
			break;
	}
?>
<br><br>

I hope that helps someone. And, if anyone knows a better way to do this, please post a reply. Thanks!

Comments

jackalope’s picture

I've been trying to figure out how to do this myself for some time, so thank you for writing this up!

I made some modifications that I thought I'd share. I have a view that displays links to other website. Each link is a node of the content type "Link", and each link can have exactly one term assigned to it from the Link Categories taxonomy ($vid = 2.) I added this code directly to the theme for the Link content type (node-link.tpl.php), since Links only show up on the Link view:

  <?php if (!$page && !$is_front): ?>
  	<?php // add headers based on taxonomy terms
  	$link_category_header = links_term_headers($node);
  	if( !isset( $GLOBALS['current_link_taxonomy_term'] ) ) {
    	$GLOBALS['current_link_taxonomy_term'] = false;
	}

	if( $GLOBALS['current_link_taxonomy_term'] != $link_category_header ) {
    	$GLOBALS['current_link_taxonomy_term'] = $link_category_header;
    	print '<h2 class="title" style="margin-bottom: 5px;">'.$link_category_header.'</h2>';
	}
	?>

And I added this to my template.php file:

// format taxonomy terms to be used as category headers on the Links view page

function links_term_headers($nid) {
$terms = taxonomy_node_get_terms_by_vocabulary($nid, 2);
	foreach ($terms as $term) {
		$link_term_header =  $term->name;
		}
	return $link_term_header;
	}

It works well for me and keeps the added code fairly concentrated.