Support

Account

Forum Replies Created

  • One quick way that comes to mind is to implement a counter (of course there may be many other ways):

    <?php $images = get_field(‘image_gallery’);
    if( $images ) { 
    $counter = 0;
    ?>
    <div id=”imagegallery”>
    
    <?php
      foreach( $images as $image ) {
        if ( $counter == 0 ) {
          echo 'first image';
        else {
          echo 'all other images';
        }
        $counter++;
     }
    ?>
    
    </div>
    <?php } ?>
  • Hi @elliot, hi @davelee

    Thanks for the solution, it’s a life saver.

    For anyone needing the full code, if you want to set the featured image when you save the post, this is also the hook:

    add_action( 'save_post', 'set_featured_image_from_gallery' );
    
    function set_featured_image_from_gallery() {
      $has_thumbnail = get_the_post_thumbnail($post->ID);
    
      if ( !$has_thumbnail ) {
    
        $images = get_field('gallery', false, false);
        $image_id = $images[0];
    
        if ( $image_id ) {
          set_post_thumbnail( $post->ID, $image_id );
        }
      }
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)