Home Forums ACF PRO Show First image of gallery


Show First image of gallery

  • Hi everyone!

    I have a serious dude. Is possible show only the first image of Gallery?

  • Hi @contacto9

    Yes, this is possible. The gallery returns an array of image data, so just get the first image $images[0]

  • I don’t understand sorry. I get gallery with the next code. How can modify for get/show only first image of gallery? Really thanks!

    $images = get_field('images');
    if( $images ): ?>
        <ul class="imagegallery">
            <?php foreach( $images as $image ): ?>
                    <a href="<?php echo $image['url']; ?>" rel="lightbox">
                         <img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
                    <?php echo $image['caption']; ?>
            <?php endforeach; ?>
    <?php endif; ?>
  • I have the solution. thanks for your help!!

    <?php if (has_post_thumbnail( $post->ID ) ): ?>
      <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
      <div id="custom-bg" style="background-image: url('<?php echo $image[0]; ?>')">
    <?php endif; ?>
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Show First image of gallery’ is closed to new replies.