Home › Forums › General Issues › How do you get the last row of a repeater?
I’m getting this error “Parse error: syntax error, unexpected ‘$end_row’ (T_VARIABLE) in…”. I’m not sure why, I’ve seen multiple examples of this is how you get the last row, but it just doesn’t work.
function smile_gallery( $atts ) {
if( have_rows('smile_gallery', 3045) ):
$i = 0;
$html_out = '';
$html_out .= '<div class="smile-container">';
$rows = get_field('smile_gallery', 3045);
$end_row = end($rows);
$recent = $end_row['smile_week'];
$html_out .= '<div class="col-md-6">';
$html_out .= '<div class="smile-recent">';
$html_out .= $recent;
$html_out .= '</div>';
$html_out .= '</div>';
$html_out .= '</div>';
endif;
return $html_out;
}
add_shortcode( 'show_smiles', 'smile_gallery' );
I fixed the issue, but I want to do something additional in this function. I want to be able to also loop through all the rows and output all except the last one. I have two columns. The left will output the last row and the right will output all but the last.
function smile_gallery( $atts ) {
if( have_rows('smile_gallery', 3045) ):
$i = 0;
$html_out = '';
$html_out .= '<div class="smile-container">';
$rows = get_field('smile_gallery', 3045);
$end_row = end($rows);
$recent = $end_row['smile_week'];
$html_out .= '<div class="col-md-6">';
$html_out .= '<div class="smile-recent">';
$html_out .= $recent;
$html_out .= '</div>';
$html_out .= '</div>';
$html_out .= '<div class="col-md-6">';
$html_out .= '<div class="smile-thumbs">';
while( have_rows('smile_gallery', 3045) ): the_row();
$week = get_sub_field('smile_week', 3045);
$img = get_sub_field('smile_img', 3045);
$i++;
// Do stuff with each post here
$html_out .= '<div class="smile-img">';
$html_out .= $week;
$html_out .= '</div>';
endwhile;
$html_out .= '</div>';
$html_out .= '</div>';
$html_out .= '</div>'; // smile container
endif;
return $html_out;
}
add_shortcode( 'show_smiles', 'smile_gallery' );
This is the full function. To take it an even step further, it would be cool to order the rows by $week. $week outputs a number.
You must be logged in to reply to this topic.
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!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PRO’s Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 2023
© 2023 Advanced Custom Fields.
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.