Home › Forums › Add-ons › Repeater Field › repeater code causes white screen of death › Reply To: repeater code causes white screen of death
In this instance the problem is in different code for using ways of starting and finishing a loop.
The standard way of doing this is to use curly brackets, which ends up with something that looks like the following
if( have_rows('repeater_field') ){
while('repeater_field'){
the_row();
the_sub_field('repeater_field_title');
}
}
The alternate way of doing this is a shorthand way of opening/closing loops. This can make the code easier to read, and helps to differentiate which brackets are doing what. Some people like it, some don’t – it’s all down to personal style really. The same example in alternate style looks like this:
if( have_rows('repeater_field') ) :
while('repeater_field') :
the_row();
the_sub_field('repeater_field_title');
endwhile;
endif;
The important thing is that you pick whichever one you prefer and stick with it. In your example – the while() loop has already opened with the alternate format, but it then opens/closes in standard format. To fix your whitescreen, removing those brackets and replacing the final one with “endwhile;” will stop the loop from hanging.
–edit–
Actually I also just noticed that you have a curly bracket opening underneath global $post; There’s no need for this, if you remove it and then remove one of the curly brackets at the end.
Often what I will do is go through a function if I’m ever lost and just count the brackets on my fingers. Add one for every curly open bracket, remove one for every closing bracket. You should end up on 0.
function sal_display_code_grid_item() {
global $post;
echo( '<div class="outside">');
echo ( '<div class ="my-header">' );
$thispicture = get_field( "cg_pretty_picture" , $post->ID );
echo( '<img src="' . $thispicture . '"/>');
echo ( '</div><!-- end header -->' ); //end header
echo( '<div class="inside">' );
/*https://www.advancedcustomfields.com/resources/have_rows*/
echo( '<ul>' );
if( have_rows('cg_the_links', $post->ID ) ) :
//tried with and without $post->ID, but I think that I need it
while( have_rows('cg_the_links', $post->ID ) ): the_row();
$thislink = get_sub_field('cg_link_url', $post->ID );
$thislinktext = get_sub_field('cg_link_text', $post->ID );
echo( '<li>' );
echo( '<a href="' . $thislink . '">' . $thislinktext . '</a>' );
echo( '</li>' );
endwhile;
endif;
echo( '</ul>' );
echo ( '</div>'); //end of inside div
echo( '</div>' ); //end of outside
}
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.