4

I have a content type which has a multivalue image field. I need to create a template that displays a list of nodes of that type but displays only one value of that multi field, not all of them together.

I know we can do if we use views under 'multiple field settings' which is explained here views 3: select only one value from a multivalue field but I am not using views, Can we do using only template nodes?

  • node title1

  • first image

  • node title2

  • second image

    etc

Please help.

2 Answers 2

0

Assuming you already have a node template for that content type already (if not then you can read more here). Within the template, instead of outputting all content in one print command, you'd break them down into the fields you need. Then you can output the image field and programmatically limit it to the first value, as described in this SE post.

1
  • Sorry this didn't help.... I have the node template of the content type. the reference post is for dependent select menu which is different for my case.
    – Ajit
    Commented Dec 21, 2015 at 9:39
0

I have fixed myself using following code:

                        for($index=0; $index<1; $index++){  

                <div class="row speaker-detail-row">        
                        <div class="field field-name-field-speakers-image field-type-image">
                                <?php
                                $fields = field_get_items('node', $node, 'field_speakers_image');
                                $speakers_image = field_view_value('node', $node, 'field_speakers_image', $fields[$index]);

                                 if (!empty($fields)) {
                                            print render($speakers_image);  
                                        }else {print "Image not found";}
                                ?> 
                        </div><!--end field-->  

                        <div class="field field-name-field-speaker-name field-type-link-field">
                            <?php 
                                $fields = field_get_items('node', $node, 'field_speaker_name');
                                $speaker_name = field_view_value('node', $node, 'field_speaker_name', $fields[$index]);

                                    if ($fields) {
                                    print render($speaker_name); 
                                            } 
                            ?>
                        </div><!--end field-->  

                    </div><!--end row--><br>
                      <?php     }?><!--end for loop-->

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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