By jasonsafro on
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
A slightly different approach
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:
And I added this to my template.php file:
It works well for me and keeps the added code fairly concentrated.