Your Location is: Home > Php

ACF Flexible Content - Looping two layouts (parse error?)

From: Wellington View: 4710 Zecele 

Question

Getting to grips with ACF and it's flexible content field.

I have two layouts as shown below but I can't seem to get the second part (image_block) working correctly as it keeps popping up with a (parse error) I'm probably missing something obvious but if anyone has some advice it would be very helpful.

<?php if( have_rows('content-h') ):?>

    <?php while ( have_rows('content-h') ) : the_row();?>

    <?php if( get_row_layout() == 'text_block' ):
            $title = get_sub_field('title');
            $description = get_sub_field('description');
            $subheading = get_sub_field('subheading');
            
        ?>

            <div class="col-lg-6">
                <div class="section-title">
                    <span class="text-color"><?php echo $subheading; ?></span>
                    <h2 class="mt-3 content-title"><?php echo $title; ?></h2>
                </div>
            </div>
            <div class="col-lg-6">
                <p><?php echo $description; ?></p>
            </div>

    <?php endif; ?>

    </div>

    <div class="row justify-content-center">
    
    <?php if( get_row_layout() == 'image_block' ):
            $image = get_sub_field('image');
            $title = get_sub_field('title');
            $description = get_sub_field('description');
            $link = get_sub_field('link');
            
            ?>

            <div class="col-lg-3 col-md-6 col-12">
                <div class="intro-item mb-5 mb-lg-0"> 
                    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" alt="" class="img-fluid w-100">
                    <h4 class="mt-4 mb-3"><?php echo $title; ?></h4>
                    <p><?php echo $description; ?></p>
                </div>
            </div>

    <?php endwhile; ?>
<?php endif; ?>

Any help is appreciated.

Best answer

This is a straight PHP parsing error, nothing to do with ACF or flexible fields.

See the comment where I believe you are missing an endif. The best thing to do in these situations is to go through your code line by line noting where you are starting a conditional and whether you have ended it correctly (so the blocks are nested).

Also check that your HTML elements are formed correctly. There appears to be an extra closing div tag - see comments in the code.

<?php if( have_rows('content-h') ):?>

    <?php while ( have_rows('content-h') ) : the_row();?>

    <?php if( get_row_layout() == 'text_block' ):
            $title = get_sub_field('title');
            $description = get_sub_field('description');
            $subheading = get_sub_field('subheading');
            
        ?>

            <div class="col-lg-6">
                <div class="section-title">
                    <span class="text-color"><?php echo $subheading; ?></span>
                    <h2 class="mt-3 content-title"><?php echo $title; ?></h2>
                </div>
            </div>
            <div class="col-lg-6">
                <p><?php echo $description; ?></p>
            </div>

    <?php endif; ?>

    </div> <!-- THIS closing div tag seems to be spurious -->

    <div class="row justify-content-center">
    
    <?php if( get_row_layout() == 'image_block' ):
            $image = get_sub_field('image');
            $title = get_sub_field('title');
            $description = get_sub_field('description');
            $link = get_sub_field('link');
            
            ?>

            <div class="col-lg-3 col-md-6 col-12">
                <div class="intro-item mb-5 mb-lg-0"> 
                    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" alt="" class="img-fluid w-100">
                    <h4 class="mt-4 mb-3"><?php echo $title; ?></h4>
                    <p><?php echo $description; ?></p>
                </div>
            </div>

       <?php endif; //THIS IS WHAT NEEDS TO BE ADDED CHECK IT IS IN THE RIGHT PLACE ?>

    <?php endwhile; ?>
<?php endif; ?>