返回值时,Wordpress ACF转发器未循环

时间:2020-02-04 15:52:28

标签: php wordpress advanced-custom-fields acfpro

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

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

1 个答案:

答案 0 :(得分: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;
相关问题