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.
Would this be easier to achieve using the gallery field?