Support

Account

Home Forums Add-ons Repeater Field repeater code causes white screen of death

Solved

repeater code causes white screen of death

  • Hi
    I’ve taken this code from the repeater documentation,

    // check if the repeater field has rows of data
    if( have_rows('repeater_field_name') ):
    
     	// loop through the rows of data
        while ( have_rows('repeater_field_name') ) : the_row();
    
            // display a sub field value
            the_sub_field('sub_field_name');
    
        endwhile;

    changed the variable names to my own, and put it into functions.php.

    When I load functions.php to my site, I get the white screen of death. By commenting lines in and out, I’ve found that it’s the line

    while ( have_rows('repeater_field_name') ) : the_row();

    that’s causing the problem. I can’t see why – I’ve just copied and pasted from the documentation.
    Any ideas?

  • Can you post the function that your while loop is a part of within your functions file, or potentially even just your functions file (although that could be exceptionally long, so if you do that maybe use http://pastebin.com/

    It could be something to do with how you’re calling in the function itself within functions instead of using that code within a template file.

    Anything you can provide will give us some clues to help 🙂

  • Hi @allonestring

    Please keep in mind that the code example in the documentation is supposed to be used in the front end template files, not functions.php file. Like What Edd said if you want to add it in functions.php file, you need to put it inside a function. Don’t forget to pass the object ID as the second parameter of the have_rows() function if you execute it in functions.php like this:

    while ( have_rows('repeater_field_name', 99) )

    Where ’99’ is the object ID (post, page, etc.). Also, could you please set WP_DEBUG to true to see any error messages on the page? Please take a look at this page to learn how to set it: https://codex.wordpress.org/Debugging_in_WordPress.

    Thanks!

  • In ACF, I’ve used field-group prefixes on all fields to try to prevent conflicts; these use cg_. Apart from the repeater field, this function is a duplicate of another that works (with a different prefix!).

    I’ve pared down the function enough for you to see how I’ve grabbed other fields and how I’m trying to grab the repeater fields. This shortened version still white-screens when I include the while have_rows line.

    <?php 
        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>' );
    
                    }
    
                }
                echo( '</ul>' );
                
                echo ( '</div>'); //end of inside div
                
                echo( '</div>' ); //end of outside
            }
        }
    ?>
    
  • 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
        
    }
  • Thank you very much! No more white screen of death!

    I’m from the curly-bracket school of coding, so have gone for your curly bracket solution.

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

The topic ‘repeater code causes white screen of death’ is closed to new replies.