繁体   English   中英

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM