Support

Account

Home Forums Add-ons Gallery Field Get last image from gallery

Solved

Get last image from gallery

  • I am trying to get the last image from a gallery (all of the galleries have different amounts of images so I can’t do this with an absolute number). Is this possible? The code below works perfectly for the first image, but the client is needing the last (most recent) image from the gallery.

    <style>
     <?php 
            $residential_images = get_field('residential_gallery',29); 
            $residential_image_1 = $residential_images[0]; 
            ?>    
    .scaleme#residential{
    		background-image:url(<?php echo $residential_image_1[url]; ?>);
    	}
    
    </style>
  • try this:

    <?php 
    	$residential_images = get_field('residential_gallery',29); 
    	$residential_image_end = end($residential_images); 
    	reset($residential_images);
    ?>
  • Thanks for the quick response! However, no image showed up and I got this error at the bottom:

    Warning: array_reverse() expects parameter 1 to be array, boolean given in /home1/bakerdes/public_html/wp-content/themes/baker/baker-portfolio-page.php on line 143

  • are you try it with [url] parameter?

    <style>
    <?php 
    	$residential_images = get_field('residential_gallery',29); 
    	$residential_image_end = end($residential_images); 
    	reset($residential_images);
    ?> 
    .scaleme#residential{
    		background-image:url(<?php echo $residential_image_end[url]; ?>);
    	}
    
    </style>

    for test it:

    print_r($residential_image_end);

  • Thanks so much! Works!

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

The topic ‘Get last image from gallery’ is closed to new replies.