views:

2350

answers:

2

I'm trying to create a page by using view 2. This page list all the nodes and grouping by their taxonomy term. In this view the Style as Unformatted and Row style as Fields. The fields as following:

Node: Title 
Node: Teaser 
Taxonomy: Term

The problem is that I want the first row under each term display both Title & Teaser and the rest display only the Title. Example:

-News

  1. Title & Teaser
  2. Title
  3. Title

-Sport

  1. Title & Teaser
  2. Title
  3. Title

-Entertainment

  1. Title & Teaser
  2. Title
  3. Title

I tried to theme by using

  • views-view-unformatted.tpl.php
  • views-view-fields.tpl.php
  • views-view-field.tpl.php

above three files with no luck. I have struggled with this issue for a while now, any help will be appreciated. Thank you.

+2  A: 

You'll need to use the 'Row style output' template (e.g. views-view-fields.tpl.php).

To make it unique to your view, you'll want to use a more specific template name than that, such as the last possible template name showed in that section - e.g. views-view-fields--myview--default.tpl.php.

Once there, you'll need you have a global variable to keep track of the current row, something like:

<?php 
  global $is_first_of_term;

  if(!isset($is_first_of_term)){
    $is_first_of_term = TRUE;
  } else {
    $is_first_of_term = FALSE;
  }


  // Then use $is_first_of_term below (unmodified original script)
  // to print whatever you want.
?>
<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>">
    <?php if ($field->label): ?>
      <label class="views-label-<?php print $field->class; ?>">
        <?php print $field->label; ?>:
      </label>
    <?php endif; ?>
      <?php
      // $field->element_type is either SPAN or DIV depending upon whether or not
      // the field is a 'block' element type or 'inline' element type.
      ?>
      <<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>>
  </<?php print $field->inline_html;?>>
<?php endforeach; ?>
Seb
Thank you very much for reply. I was surprised how quick your response ? I tried but still have no idea how to use $fields array to tell which row is first row. Any addtional code will be great help. Thanks for your time and I'm really appreciated.
mesmer
Hi mesmer. I've updated my answer. I told you to use the $fields array but it was not necessary: you can do it like I've posted now. Take a look and let me know.
Seb
Hello Seb. Thank you for the update. I have tried new code. Since I use grouping by node's taxonomy term, new code only works on very first node. It doesn't show the right way for every first row under each term. Any ideas? Thanks again for helping me with this issue.
mesmer
Aaarrgh you're right, that's why I've told you to use the $fields array in the first place, but I forgot! Hehe look, the $fields array contains all the fields for current row. What you want to do is inspect each one of them and see if it belongs to a different term than the previous one. So what you can do is set $is_first_of_term to TRUE only if you see the present row's term is different from the previous. Having a new variable called $prev_term will help you with that. Also, I'd suggest doing a `var_dump($fields)` to see the contents of the variable, so you understand how it works :)
Seb
Hello Seb, by following your suggestion, I finally get the way I wanted for long time. Thank you very much! Now I can restart the project I had pushed back while ago just because this. Now I get it working and the answer seems so simple. WHY I DIDN'T THINK OF THAT? I keep asking myself : ) Thanks again for all your help!
mesmer
Hey mesmer, glad I could help! :) If my answer is the solution for your answer please mark it as such, so others coming with the same problem know how to solve it :)
Seb
nice...............
Paniyar
A: 

Setup each view display as 'content pane'. Use a panel (panels3 module) with multiple views, first view has title and teaser (limit 1), next view is just titles (limit 2 or 3). You can 'stack' multiple views in the same panel.

Repeat for each taxonomy; use taxonomy for arguments.

Kirk Hings

related questions