Support

Account

Home Forums General Issues Image size for $last_row

Solved

Image size for $last_row

  • Hello

    I am using the repeater function from another page (ID 124) to display content from the last item in the repeater list (through Last Row) onto the index page. I use this code:

    <?php $repeater = get_field('newReleases', 124); $last_row = end($repeater); { ?>
    	<a href="<?php echo get_permalink(124); ?>" title="<?php echo $last_row['Title']; ?>"><img src="<?php echo $last_row['Image']; ?>" alt="<?php echo $last_row['Title']; ?>"></a>        
    <?php } ?>

    It works as intended, but I would like to also get the image width and height attributes too so I can add these into the IMG tag. Normally the php code to do this is:

    width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>"

    but I cant seem to get this to work in the $last_row calls. Is this possible to do?

    Also I wonder, is it possible to call the Medium size of the image, rather than just the URL to the full-size image?

    Many thanks,

    Allen

  • Hi @artdivision

    If you set the Return Value option to “Image Array”, You should be able to do it like this:

    <?php
    
    // get the image
    $image = $last_row['Image'];
    
    // get the medium image
    $size = 'medium';
    $medium = $image['sizes'][ $size ];
    $width = $image['sizes'][ $size . '-width' ];
    $height = $image['sizes'][ $size . '-height' ];
    
    ?>
    
    <img src="<?php echo $medium; ?>" alt="<?php echo $last_row['Title']; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" >

    This page should give you more idea about it: https://www.advancedcustomfields.com/resources/image/.

    I hope this helps 🙂

  • Hello James

    That’s great. Just one question; in the ACF field editor options for Image field type, I have for Return Value choices for Image Object, Image URL and Image ID. Which one can I use for Image Array. Is it Image Object?

    Many thanks,

    Allen

  • It’s Image Object. Works like a treat now.

    Many thanks.

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

The topic ‘Image size for $last_row’ is closed to new replies.