Home › Forums › Add-ons › Repeater Field › First row of repeater field that returns Image Object
How do I return the first row in a repeater field that returns the Image Object instead of the ID for an image?
I see how to do it if the image field is set to return the ID but I am unsure how to do it if the image field is set to return the Image Object.
Thanks!
Set the last argument for get_field() to false, the second one too if you don’t need to specify the post id.
get_sub_field('field_name', false, false);
will return the unformatted field value (ID) of for the field on the current post.
Thanks! I am not quite following how to integrate this with the demo code for finding the first row of a repeater field found here:
This is the code I have so far but I am not getting the image url to echo into the inline style for background-image on the mobile-bg div.
<?php
$rows = get_field('slider'); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['slide']; // get the sub field value
// Note
// $first_row_image = 123 (image ID)
$image = wp_get_attachment_image_src( $first_row_image, 'full' );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<div class="mobile-bg visible-xs" style="background-image:<?php echo $image[0]; ?>"></div><!-- end mobile-bg -->
I don’t think I need this code $image = wp_get_attachment_image_src( $first_row_image, 'full' );
but I don’t know what to put in its place.
Thanks!
Well, I wasn’t thinking about getting a repeater in that fashion, I was assuming that you’d be using a ‘while(have_rows())’ loop.
You have several choices. You can get the entire repeater with no formatted values
$rows = get_field('slider', false, false);
and then do all the formatting yourself,
or you can get the repeater twice
$formatted_rows = get_field('slider');
$unformatted_rows = get_field('slider', false, false);
That worked! Thank you! My code may not be as elegant as it should be. Here is what I did:
<?php if( have_rows('slider') ): ?>
<?php while( have_rows('slider') ): the_row();
$image = get_sub_field('slide');
$rows = get_field('slider', false, false); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['slide']; // get the sub field value
?>
<div class="mobile-bg visible-xs" style="background-image:<?php echo $image['url']; ?>"></div><!-- end mobile-bg -->
<?php break; ?>
<?php endwhile; ?>
<?php endif; ?>
I am open to suggestions to make my code better.
Thanks again!
It looks good to me, I can look at it and understand what’s going on. To me that’s far more important than being “elegant”. Most of the time “elegant” means, over complicated for the purpose of impressing the next person that needs to work on it, and I’m far more impressed if I can figure out what’s going on and get the job done quickly. 🙂
The topic ‘First row of repeater field that returns Image Object’ 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.