Francis Alvin Tan
Francis Alvin Tan

Reputation: 1087

Add a section before the last row ACF get_row_layout()

Need help on this guys as I am trying to add a section before the last row, I am using ACF flexible content and the rows can be interchangeable, so even if I change it still the section that I added will still be always before the last row

if( have_rows($content_rows) ):
    while ( have_rows($content_rows) ) : the_row();

        if( get_row_layout() == 'row_one' ):
            echo 'First row';
        endif;

        if( get_row_layout() == 'row_two' ):
            echo 'Second row';
        endif;

        {need to add a div here and it should show before the last row even if its moved}

        if( get_row_layout() == 'row_three' ):
            echo 'Third row';
        endif;

    endwhile;
endif;

Upvotes: 2

Views: 797

Answers (1)

Matthijs Knigge
Matthijs Knigge

Reputation: 402

The trick is to count all the rows and output before te last row the div

if( have_rows($content_rows) ):
    $totalCount = count(get_field($content_rows));
    $currentCount = 1;
    while ( have_rows($content_rows) ) : the_row();

        //add div before the last row
        if ($currentCount - 1 === $totalCount) {
            echo "<div></div>";
        }

        if( get_row_layout() == 'row_one' ):
            echo 'First row';
        endif;

        if( get_row_layout() == 'row_two' ):
            echo 'Second row';
        endif;

        if( get_row_layout() == 'row_three' ):
            echo 'Third row';
        endif;

        $fields_count++;
    endwhile;
endif;

Upvotes: 1

Related Questions