Wordpress ACF 转发器在返回值时不循环

Wordpress ACF repeater not looping when returning values

出于某种原因,我的循环将只显示转发器的第一行。我怎样才能让循环为所有已添加的行创建链接?

function related_pages_shortcode2() {
    if( have_rows('related_pages') ):
    while( have_rows('related_pages') ): the_row(); 

    $type = get_sub_field('type');
    $name = get_sub_field('name');
    $link = get_sub_field('url');

    $related_page = '<strong>' . $type . ': </strong>' . '<a href="' . $link . '">' . $name . '</a>';

    return $related_page;

    endwhile;

else :

endif;
}

add_shortcode( 'related_pages2', 'related_pages_shortcode2' );

您正在返回函数(阻止函数进一步执行),而不是保存先前的输出并向其追加新行。像那样更改您的函数,以便它也保存 while 循环中生成的先前内容:

    if( have_rows('related_pages') ):
        $output = ''; // initialize the output buffer as an empty string
        while( have_rows('related_pages') ): the_row(); 

            $type = get_sub_field('type');
            $name = get_sub_field('name');
            $link = get_sub_field('url');

            $related_page = '<strong>' . $type . ': </strong>' . '<a href="' . $link . '">' . $name . '</a>';

            $output .= $related_page; // in this way you append the new row to the previous output

        endwhile;

    endif;