Support

Account

Home Forums Add-ons Gallery Field Getting Image URL when using Gallery that's returning ID

Solved

Getting Image URL when using Gallery that's returning ID

  • Heya,

    I’m using the Gallery field returning Image ID and need to figure out how to get the image’s standalone URL. Purpose for doing so is we need to link each image’s instance so we can present the click in a modal lightbox. It’s vital that we return Image ID since we are using a third party plugin that allows users to submit content, and it only seems to work when we return the values as Image ID.

    Here’s what I was hoping would work but doesn’t so far, everything works as it should except for the URL doesn’t print in the a href…

    
    <?php 
    $images = get_field('upload_submission_image_gallery');
    $url = $image['url'];
    $size = 'uncropped'; // (thumbnail, medium, large, full or custom size)
    if( $images ):
    	  ?>
    	<ul class="image-gallery">
    		<?php foreach( $images as $image_id ): ?>
    			<li>
    			
    				<a href="<?php echo esc_url($image['url']); ?>"  data-lightbox="gal[0]">
    				<?php echo wp_get_attachment_image( $image_id, $size ); ?>
    				</a>
    			</li>
    		<?php endforeach; ?>
    	</ul>
    <?php endif; ?>
    

    Any ideas on a fix or alternate strategy?

    Thanks for your time!

  • You are returning the image id but you are attempting to use the value as if you are returning an array echo esc_url($image['url']);.

    When returning the ID you need to use WP functions to get the image, like wp_get_attachment_image_src()

  • OK great thanks kindly John, that solved it!

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

You must be logged in to reply to this topic.