Home › Forums › General Issues › 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
The topic ‘Image size for $last_row’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.