Support

Account

Home Forums Front-end Issues gallery Array Reply To: gallery Array

  • A gallery field always returns an array of images/image data. An explanation of this is in the documentation https://www.advancedcustomfields.com/resources/gallery/.

    A gallery field is one of the more advanced field types. The values of these fields cannot be simply echoed on the page, and they are not really appropriate for use with get_fields() and looping over all the fields that are returned. In such a loop you would need to test for the different fields and do something different with these special fields.

    
    <?php
      $fields = get_fields($post->ID);
      if ($fields) {
        ?>
          <ul>
            <?php 
              foreach ($fields as $name => $value) {
                ?>
                  <li>
                    <b><?php echo $name; ?></b>
                    <?php 
                      if ($name == 'THE_NAME_OF_YOUR_GALLERY_FIELD') {
                        // do something special for gallery fields
                      } else {
                        // basic field
                        echo $value;
                      }
                    ?>
                  </li>
                <?php 
              } // end foreach fields
            ?>
          </ul>
        <?php 
      } // end if fields
    ?>