Home › Forums › Add-ons › Flexible Content Field › Flexible – Add code to last row entry
After searching around I have a counter to count the number of entries of rows of a flexible content. The counter will insert a div before the number of entries. What I am trying to resolve is a way to add code after the last row entry?
<?php
if( have_rows('cm_content') ):
$counter = 0;
while ( have_rows('cm_content') ) : the_row();
$counter++;
if($counter == 9 ){
echo '<div class="col-xs-12">break</div>';
}
if( get_row_layout() == 'iu_image_url' ):
$image = get_sub_field('iu_image');
$imageurl = get_sub_field('iu_url');
echo '
<div class="col-xs-6 col-sm-3">
<div class="small_image_box">
<a href="' . $imageurl . '" target="_blank">
<img src="' . $image . '" alt="' . '" class="img-responsive center-block" />
</a>
</div>
</div>
';
elseif( get_row_layout() == 'ilm_image_media' ):
$image = get_sub_field('ilm_image');
$imageurl = get_sub_field('ilm_url');
echo '
<div class="col-xs-6 col-sm-3">
<div class="small_image_box">
<a href="' . $imageurl . '" target="_blank">
<img src="' . $image . '" alt="' . '" class="img-responsive center-block" />
</a>
</div>
</div>
';
endif;
endwhile;
else :
endif;
?>
I was able to solve this by finding the total amount of rows in your field by using get_field_object
.
In your code, after if( have_rows('cm_content') ):
try this:
$field_object = get_field_object('cm_content');
$total_rows = count($field_object['value']);
Then you can test for it with an if statement:
if($counter == $total_rows ){
// this is the last one!
}
Cheers,
Andre
The topic ‘Flexible – Add code to last row entry’ 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.