0

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

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' );
4

1 回答 1

0

您正在返回函数(阻止函数进一步执行),而不是保存先前的输出并将新行附加到它。像这样更改您的函数,以便它还保存在 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;
于 2020-02-04T15:54:34.670 回答