【问题标题】:Wordpress ACF repeater not looping when returning values返回值时Wordpress ACF转发器不循环
【发布时间】:2020-05-20 11:56:28
【问题描述】:

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

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

【问题讨论】:

    标签: php wordpress advanced-custom-fields acfpro


    【解决方案1】:

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

    【讨论】:

    • 啊,是的,谢谢!我现在唯一的问题是,它不是将它返回到它放置的确切位置,而是回声到身体的顶部。有没有办法让它回显到返回的 div 中?
    • @jguiles 我已根据您的详细描述修改了我的答案。
    猜你喜欢
    • 2014-07-17
    • 2014-09-13
    • 2019-11-16
    • 1970-01-01
    • 2019-11-10
    • 2015-04-22
    • 2016-05-10
    • 2015-12-09
    • 1970-01-01
    相关资源
    最近更新 更多