Support

Account

Home Forums Add-ons Repeater Field Get Data from Last Row in Repeater

Solved

Get Data from Last Row in Repeater

  • Hello, I’m having trouble trying to get values from the last row of a repeater field. There is and always will be 4 rows.

    I need to:

    • get a text sub field
    • if radio button value equals ‘x’…
      • get image sub field (with thumbnail size)
      • if url sub field, wrap image in a link
    • if radio button equals ‘y’…
      • get oembed sub field
    • get wysiwyg editor

    I’ve read through and tried the examples in the documentation as well as some I found in the forum but I’m having a lot of trouble getting it right. Any tips are greatly appreciated.

    Here’s where I’m at so far in my fumbling tests:

    
    $rows = get_field('pods' ); // get all the rows
    if( have_rows('pods') ):
    	while ( have_rows('pods') ) : the_row();
    		$rows = get_field('pods' ); // get all the rows
    		$first_row = $rows[3]; // get the last row
    		$first_row_image = $first_row['pod_image' ]; // 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];
    		?>
    		<img src="<?php echo $image[3]; ?>" />
    	<?php endwhile;
    endif;
    
    
  • Hi @inhouse

    This is a bit confusing… 😉

    So you want to do these steps

    get a text sub field
    if radio button value equals ‘x’…
    get image sub field (with thumbnail size)
    if url sub field, wrap image in a link
    if radio button equals ‘y’…
    get oembed sub field
    get wysiwyg editor

    ONLY for the last row in your repeater?

  • Hi Jonathan, yes you understand my confusing question! I think if I had a couple of the listed items worked out for me I should be able to continue the logic to the remaining items. I don’t expect you to write it all out for me but any tips are much appreciated!

  • Okay 🙂

    So to work with just the last row you can easily do something like:

    
    <?php
    $repeater = get_field('repeater');
    $last_row = end($repeater);
    
    echo $last_row['sub_field'];
    ?>
    
  • Thanks, that’s insanely easy compared to the documentation and other examples I found!

    Admittedly this is more of a PHP issue than ACF so I’ll understand if this next question goes unanswered! One thing that’s tripping me up is using the advanced image vars. How do I use the $last_row variable in conjunction with the image title, alt, size vars? My attempt with ‘url’ was unsuccessful 🙂

    
    if ( $last_row['pod_image'] ) {
    	$url = $last_row[ $pod_image['url'] ];
    	$title = $pod_image['title'];
    	$alt = $pod_image['alt'];
    	// thumbnail
    	$size = 'medium';
    	$thumb = $pod_image['sizes'][ $size ]; ?>
    	<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" />
    }
    
  • No problem!

    You’re almost there. But notice that you try to use a variable $pod_image without first declaring it and setting its value.

    
    
    <?php if ( $last_row['pod_image'] ) { ?>
    	<?php $pod_image = $last_row['pod_image']; ?>
    	<img src="<?php echo $pod_image['sizes']['medium']; ?>" alt="<?php echo $pod_image['alt']; ?>" />
    <?php } ?>
    
  • And I overcomplicated things yet again. This works great and makes perfect sense! Thanks so much for your help Jonathan! Above and beyond fantastic support!

  • No problem 🙂 Overcomplicating things tend to happen in code! That’s why a lot of developers try to refactor their code while developing!

    Have a nice weekend

  • When using this with a repeater that is IN a Flexible Content field.. I get this error:
    Warning: count(): Parameter must be an array or an object that implements

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

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.