首页 > 解决方案 > 在最后一行之前添加一个部分 ACF get_row_layout()

问题描述

需要帮助这些人,因为我试图在最后一行之前添加一个部分,我使用的是 ACF 灵活内容,并且这些行可以互换,所以即使我更改它,我添加的部分仍然会始终在最后一行之前排

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;

标签: phpwordpresswhile-loopadvanced-custom-fields

解决方案


诀窍是在最后一行 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;

推荐阅读