user1372359
user1372359

Reputation:

My code causing infinite loop, can't see the bug

I am working on a WordPress project, using the Advanced Custom Fields plugin.

However I am getting a crazy infinite loop from my code, so I've obviously messed things up somewhere, however after a good two hours looking at it I can't see the problem.

<?php get_header(); ?>

    <?php
    // If the loop has posts
    if (have_posts()) :
        // For each post this page has
        while (have_posts()) : the_post();

            // Display the Flexible Content
            while(has_sub_field('add_row')):

                // If the content type is Tagline
                if(get_row_layout() == 'tagline'): ?>
                    <div class="row paddingBottom paddingTop">
                        <div class="col-xs-12">
                            <h3 class="tagLine"><?php the_sub_field('tagline'); ?></h3>
                        </div>
                    </div>
                <?php
                // If the content type is a Featured Image
                /*elseif ( get_row_layout() == 'featured_image' ): ?>
                    <div class="col-xs-12 textCenter">
                        <?php   
                            $attachment_id = get_sub_field('featured_image');
                            $size = "full-width"; // (thumbnail, medium, large, full or custom size)
                            $image = wp_get_attachment_image_src( $attachment_id, $size );
                        ?>
                        <img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" />
                    </div>
                <?php*/
                // If the content type is a horizontal rule
                elseif ( get_row_layout() == 'horizontal_rule' ) : ?>
                <div class="row">
                    <div class="col-xs-12">
                        <hr />
                    </div>
                </div>
                <?php   
                // If the content type is Columns
                elseif ( get_row_layout() == 'columns' ):
                    // If there is at least one column section
                    if(get_sub_field('column')):
                        // For each column section
                        while(has_sub_field('column')): ?>
                            <div class="row paddingTop paddingBottom"> 

                                <?php
                                    if(get_sub_field('column_content_type')):
                                        $count_rows = 0;
                                        // Counting
                                        while(has_sub_field('column_content_type')):
                                            $count_rows++;
                                        endwhile;
                                        // Loop
                                        while(has_sub_field('column_content_type')): ?>
                                            <div class="
                                                <?php /*
                                                    if ( $count_rows == 1 ) :
                                                        echo "col-xs-12";
                                                    elseif ( $count_rows == 2 ) :
                                                        echo "col-xs-6";
                                                    elseif ( $count_rows == 3 ) :
                                                        echo "col-xs-6 col-sm-4";
                                                    elseif ( $count_rows == 4 ) :
                                                        echo "col-xs-6 col-sm-6 col-md-4";
                                                    endif; */
                                                ?>
                                            "> <?php
                                                // Title
                                                if ( get_sub_field('title') ) : ?>
                                                    <h2 class="smallTitle"><?php the_sub_field('title');?></h2><?php
                                                endif;
                                                // Text Area
                                                if ( get_sub_field('text') ) :
                                                    the_sub_field('text');
                                                endif;
                                                // Link
                                                if ( get_sub_field('link') ) : ?>
                                                    <?php // eventually need to make it so you can change the link text ?>
                                                    <a class="textCenter" href="<?php the_sub_field('link'); ?>">
                                                        More info
                                                    </a> <?php
                                                endif; ?>
                                            </div> <?php
                                        endwhile;
                                    endif;
                                ?>

                            </div>                      

                            <?php
                        endwhile; // end for each column section
                    endif; // end checking for at least one column section

                endif; // end checking content type is columns

            endwhile; // end flexible content

         endwhile; // end for each post
    endif; // end checking for at least one post
    ?>

<?php get_footer(); ?>

Upvotes: 0

Views: 590

Answers (2)

wiscWeb
wiscWeb

Reputation: 213

while(has_sub_field('column_content_type')):
     $count_rows++;
endwhile;

That's your problem... But it could be one of many based on what I'm seeing. It looks like you think while loops work like if statements... All you are doing in that loop is incrementing it over and over. It will never end.

Upvotes: 1

Hariprasad
Hariprasad

Reputation: 1

I think the mistake is // Display the Flexible Content while(has_sub_field('add_row')): i think you need to use if(){ // Code } instead of "while"

Upvotes: 0

Related Questions