Support

Account

Home Forums Add-ons Gallery Field First Image In Gallery As Featured Image

Solved

First Image In Gallery As Featured Image

  • Is there a way to set the first image in a gallery as the featured image for the post?

  • There is an example here of doing this with an image field https://support.advancedcustomfields.com/forums/topic/set-image-as-featured-image/ the difference is that you’d need to get the first image form the gallery instead of using the value of an image field.

  • I found another post with the solution.

    
    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('car_gallery', false, false);
        $image_id = $images[0];
    
        if ( $image_id ) {
          set_post_thumbnail( $post->ID, $image_id );
        }
      }
    }
    
    • edarioq

    • October 28, 2016 at 4:17 pm

    Figured it out?

    <?php echo $gallery[0]['url'] ?>

    • edarioq

    • October 28, 2016 at 4:25 pm

    Another question, how do I remove the first image from the gallery, I get the first image repeating itself.

    <?php if ( have_rows('apartamento_1_alcoba') ): ?>
    <div id="img-module-slider-1" class="vivienda-slider">
    
      <?php while( have_rows('apartamento_1_alcoba') ) : the_row();
        $icono_1_alcoba = get_sub_field('icono');
        $gallery = get_sub_field('lightbox');
        $id_1_alcoba = get_sub_field('plano_id');
      ?>
    
      <section data-featherlight-gallery data-feather-filter="a">
        <a class="apt-gallery-<?php echo $id_1_alcoba ?>" href="<?php echo $gallery[0]['url'] ?>">
          <img class="module-img-size" src="<?php echo $icono_1_alcoba['url'] ?>" alt="<?php echo $icono_1_alcoba['alt'] ?>">
        </a>
        <?php if ( $gallery ): ?>
          <?php foreach ( $gallery as $image ): ?>
            <a class="apt-gallery-<?php echo $id_1_alcoba ?>" href="<?php echo $image['url'] ?>" alt="<?php echo $image['alt']?>"></a>
          <?php endforeach; ?>
        <?php endif; ?>
      </section>
      <?php endwhile; ?>
    
    </div>
    <?php endif; ?>
  • 
    $gallery = get_sub_field('lightbox');
    
    // later on
    // remove the first one
    $toss_the_first_one = array_shift($gallery);
    

    http://php.net/manual/en/function.array-shift.php

    • Vision

    • September 5, 2020 at 9:16 pm

    My them is Avada and, for post submission my them accept multiple featured images to build slider. I created a form to user submit post front-end. For it, I created a gallery field with ACF and user can upload their post featured images in gallery field. My problem is, I can not map uploaded images of ACF gallery field to featured images of user created post. is it possible to help me?

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.