I am getting five instances items in my View row when I wrap image field inside my link field. This is what I have in the template file:

    <?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <?php print $field->wrapper_prefix; ?>
    <?php print $field->label_html; ?>
    <?php //print $field->content; ?>
    <div class="myRow">
        <div class="myImg">
            <span class="featuredVideo"></span>
            <a href="<?php print $fields['field_news_link']->content; ?>" target="_blank"><?php print $fields['field_news_image']->content; ?></a>
            <?php //print $fields['field_news_image']->content; ?>
        <a href="<?php print $fields['field_news_link']->content; ?>"><?php print $fields['field_news_source']->content; ?></a>
  <?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>

If I uncomment <?php //print $field->content; ?> and remove the linked image and the rest of my mark up, I don't get multiple instances of the item.

  • Try using $field and not $fields for the output.
    – enzipher
    Jul 16 '15 at 20:27
  • It breaks the page. I get these errors: Notice: Undefined variable: field in include() (line 31 of /templates/views-view-fields--featured-news--block-1.tpl.php). Warning: array_keys() expects parameter 1 to be array, null given in include() (line 31 of /templates/views-view-fields--featured-news--block-1.tpl.php).
    – srodrig
    Jul 16 '15 at 20:55
  • You can actually output the fields as you do, but in that case you should remove/comment out the foreach ... and endforeach rows. See views-view-fields.tpl.php
    – enzipher
    Jul 17 '15 at 6:53
  • Thanks, that did it! Pretty obvious fix that I missed. If you provide this as an answer I'd be happy to approve it. Thanks again!
    – srodrig
    Jul 17 '15 at 13:32
  • You're welcome! Glad I could be of help. I've added the solution as an answer.
    – enzipher
    Jul 17 '15 at 17:41

The field template prints all the fields as a row. If you want to output the row of fields manually you need to remove the foreach iteration of the fields.

See views-view-fields.tpl.php for more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.