Support

Account

Home Forums Add-ons Gallery Field display first gallery picture on archive page

Solved

display first gallery picture on archive page

    • davelee

    • October 30, 2013 at 6:45 pm

    Hi there I create a Gallery-Field to display multiple pictures on posts.
    get_field('gallery')
    but when I have used the gallery field with the first picture on the home page I have very bad performance.
    home.php:

    $images = get_field('gallery');
    $image = $images[0];
    $imageid = $image['id'];
    if( $imageid != '' ) { 
    echo '<img src="' .$image['sizes'][$imagesize].'" alt="'. $image['alt'].'" />'; // display only first image
    			 }
    

    Any ideas?
    Is there a way to save the first picture of the gallery-field in the post_thumbnail function?

    • Elliot

    • October 31, 2013 at 9:59 am

    Hi @davelee

    Yes, this is possible, however you will need to write some custom PHP to hook into the save function and then update the post’s featured thumbnail.

    How many ‘first images’ are you showing? Just the one, or are there multiple posts?
    The bad performance most likely comes from ACF having to return all the image data such as crop sizes.

    You can get the root value of the gallery field by using the $format_value param like so:

    $images = get_field('gallery', false, false);

    This will give you an array of ID’s (much faster)

    But you will then need to modify your code to load the image src / alt manually. You can find examples of this on the image field docs page.

    Good luck

    Thanks
    E

    • davelee

    • October 31, 2013 at 6:40 pm

    Hi Eliot, thanks for the Reply!

    Wow, ist so simple ๐Ÿ™‚

     	$images = get_field('gallery', false, false);
     	$image_id = $images[0];
     	if ( $image_id ) {
    	set_post_thumbnail( $post->ID, $image_id );
     	}
    
    

    But Can i used the ACF Field in the function.php
    Is this not new feature in ACF 4.3 ?

    • Elliot

    • November 1, 2013 at 11:41 am

    Hi @davelee

    Currently, to use ACF in your functions.php file, you need to use it AFTER the init action.

    In 4.3, yo ucan use it in the root of the file!

    Thanks
    E

  • 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 );
        }
      }
    }
    
  • Hi, I’ve just tried to code above and it seems to work perfectly

    One quick question I show my featured image as a large product image then have the others as thumbnails in a gallery

    Is there a way to exclude the featured image from the returned gallery results?

    This is the code I’m using to display the gallery

    Thanks

    Lee

    <?php $images = get_field(‘image_gallery’);
    if( $images ) { ?>
    <div id=”imagegallery”>

    <?php foreach( $images as $image ) { ?>

    ” alt=”<?php echo $image[‘alt’]; ?>” class=”alignleft” />

    <?php } ?>

    </div>
    <?php } ?>

  • 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 } ?>
  • Thanks Marius

    That works as a temporary solution, the only downside is if the client reorders the gallery, the featured image may not be the first in the array, ideally the solution would use the id of the featured image and check that?

    Lee

  • For anyone else looking for this I’ve found a solution, probably not the best but it works!

    The featured image code higher in the page sets an ‘$image_url’ here:

    <?php
    $image_id = get_post_thumbnail_id();
    $image_url = wp_get_attachment_image_src($image_id,’large’);
    $image_url = $image_url[0];

    echo ‘‘;

    the_post_thumbnail(‘medium’);

    echo ‘‘; ?>

    Then I just compare the gallery image to that and hide if they are the same

    <?php $images = get_field(‘image_gallery’);
    if( $images ) { ?>
    <div id=”imagegallery”>

    <?php foreach( $images as $image ) {

    if ($image_url != $image[‘url’]) {
    ?>
    ” rel=”shadowbox[gallery]” >” alt=”<?php echo $image[‘alt’]; ?>” class=”alignleft” />

    <?php } //End URL Check (to hide featured in gallery)
    } // End image loop ?>

    </div>
    <?php } ?>

    UPDATE – Editor has made a bit of a mess of the above, but you get the principal!

    Lee

    • davelee

    • September 28, 2014 at 9:59 pm

    Hi @elliot, your tipp

    $images = get_field('gallery', false, false);
    

    works only with ACF4. With ACFPro now I get the the full Array from the gallery field.
    Do you have a tip for me, how do I get a simplified array with attachment id’s?

    Thanks Davelee

    • Seath

    • December 6, 2014 at 8:39 am

    This is how I get first image from gallery.

    
    <?php 
    $images = get_field('gallery_images');
        if( $images ): 
            $image_1 = $images[0]; 
    ?>                
                <a href="<?php the_permalink(); ?>">
                    <img src="<?php echo $images[0]['sizes']['medium']; ?>" alt="<?php echo $image['alt']; ?>" />
                </a>
    <?php endif; ?>	
    

    Hope it helps ๐Ÿ˜‰

Viewing 11 posts - 1 through 11 (of 11 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.