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


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 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 );
  • Figured it out?

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

  • 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'] ?>">
        <?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; ?>
      <?php endwhile; ?>
    <?php endif; ?>
    $gallery = get_sub_field('lightbox');
    // later on
    // remove the first one
    $toss_the_first_one = array_shift($gallery);

  • 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)

The topic ‘First Image In Gallery As Featured Image’ is closed to new replies.