menu

Questions & Answers

ACF : While loops twice

I'm stuck with this loop issue. I've read many posts, asked ChatGPT, but I can't find out. My final goal is to display a flexible content within another flexible content with ACF Pro.

Here's my code :

<?php
        if ( have_rows('content-text') ) :
            while ( have_rows( 'content-text' ) ) : the_row();

                $title_heading = get_sub_field('title-heading');
                $title = get_sub_field('title');
                $text = get_sub_field('text');


                if ($title_heading == 'h2' ) {
                    echo '<h2 class="border-left">' . $title . '</h2>';
                }
        
                else {
                    echo '<h3 class="border-vertical">' . $title . '</h3>';
                }

                echo $text;
        
            endwhile;

        endif;

    ?>

On the front end, when $title_heading is equal to 'h2', it still displays this :

<h3 class="border-vertical"> </h3>

Where is the issue ?

Many thanks !

Comments:
2023-01-18 23:01:11
I'd suggest a bit of debugging. Try to echo $title_heading somewhere to see what it actually contains
2023-01-18 23:01:11
"I'm stuck with this loop issue. I've read many posts, asked ChatGPT, but I can't find out." doesn't really help, instead describe your search efforts to find helpful content from this site including links to the most relevant posts explaining why they didn't help you.
Answers(0) :