Support

Account

Home Forums Front-end Issues Multiple image sizes from Image field

Solved

Multiple image sizes from Image field

  • Hi
    I’m using this plugin Bigger Picture to implement a Lightbox on my website.

    However the mark-up requires multiple image sizes to work correctly…

    <div id="images">
      <a
        href="example.jpg"
        data-img="example.jpg"
        data-thumb="example_thumb.jpg"
        data-alt="Example"
        data-height="2500"
        data-width="1667"
      >
        <img src="example_thumb.jpg" alt="Example" />
      </a>
    </div>

    I’m currently using Image ID as the return value. I’m able to fill the field with the following code, providing a full return…

    <?php $image_file = get_sub_field( 'image_file' ); ?>
    <?php $size = 'overview'; ?>
    <?php if ( $image_file ) : ?>
    	<?php echo wp_get_attachment_image( $image_file, $size ); ?>
    <?php endif; ?>

    But I need to return just the image url, at specific sizes for the data-thumb and data-img field. I have two custom sizes setup for this, ‘overview’ and ‘hyper’.

    Is it possible to return multiple images sizes and variations in return values from just one field?

    Any help would be greatly appreciated, many thanks in advance!

  • I would use wp_get_attachment_image_src() to get each image size URL.

  • Hi John
    Thanks for the swift reply — I’ve tried to digest the link you provided and came up with this as the solution…

    
    <?php $image_file = get_sub_field( 'image_file' ); ?>
    <?php $size = 'overview'; ?>
    <?php if ( $image_file ) : ?>
    
    <?php wp_get_attachment_image_src( $image_file,  $size = 'overview',  $icon = false );?>
    
    <?php endif; ?>

    However nothing displays, altering the original string to…

    <?php echo wp_get_attachment_image_src( $image_file, $size ); ?>

    outputs ‘Array’ in the field, so believe that must be closer to the solution?

  • 
    $image = wp_get_attachment_image_src( $image_file, $size );
    $url = $image[0];
    
  • Thanks John, I replaced your response into the query but still not getting a URL (or anything returned) still – did I got the below right?

    <?php $image_file = get_sub_field( 'image_file' ); ?>
    <?php $image = wp_get_attachment_image_src( $image_file, $size );?>
    <?php $url = $image[0]; ?>
    <?php $size = 'overview'; ?>
    
    <?php if ( $image_file ) : ?>
    
    <?php wp_get_attachment_image_src( $image_file,  $size = 'overview',  $icon = false );?>
    
    <?php endif; ?>
  • Managed to achieve this using this reference… https://gist.github.com/galengidman/9840441

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

You must be logged in to reply to this topic.