首页 > 解决方案 > 将嵌套 ACF 中继器字段输出为简码中的表

问题描述

我正在尝试通过 elementor 页面上的简码在表格中显示嵌套的 ACF 字段。

我尝试将短代码与我在谷歌上找到的一些代码合并,但我没有成功。

转发器部分取自此链接: https: //support.advancedcustomfields.com/forums/topic/help-with-creating-a-table-using-nested-repeaters/

当我使用简码时,什么都没有。

function menu_loop() {
    ob_start();
    ?> 

     <?php if ( have_rows('menu') ):

        while ( have_rows('menu') ) : the_row(); ?>

            <h2 class="menu-title"><?php the_sub_field('week'); ?><h2>

            <?php if ( have_rows('week') ): ?>

                <table>

                    <thead>
                        <tr class="menus-row">
                            <td>Days</td>
                            <td>Snack AM</td>
                            <td>Lunch</td>
                            <td>Snack PM</td>
                        </tr>
                    </thead>

                    <?php while ( have_rows('week') ) : the_row(); ?>

                        <tr class="menu-row">
                            <td><?php the_sub_field('days'); ?></td>
                            <td><?php the_sub_field('snack_am'); ?></td>
                            <td><?php the_sub_field('lunch'); ?></td>
                            <td><?php the_sub_field('snack_pm'); ?></td>
                        </tr>

                    <?php endwhile;?>

                </table>

            <?php endif;?>

        <?php endwhile;?>
        <?php endif; ?>

    <?php
    return ob_get_clean();
}
add_shortcode('menushortcode', 'menu_loop');

标签: phpwordpressadvanced-custom-fields

解决方案


部分解决.. 进行更多研究/挖掘我注意到,由于这些设置即将出现在 ACF 选项页面,我们需要在代码的某些部分添加一个“选项”,这里是工作代码

既然我已经正确显示了所有内容,有没有办法将每个“周”显示为自己的选项卡和它自己的对应表?


function menu_loop() {
    ob_start();
    ?> 

     <?php if ( have_rows('menu','option') ):

        while ( have_rows('menu','option') ) : the_row(); ?>

            <h2 class="menu-title"><?php the_sub_field('week'); ?><h2>

            <?php if ( have_rows('week','option') ): ?>

                <table>

                    <thead>
                        <tr class="menus-row">
                            <td>Days</td>
                            <td>Snack AM</td>
                            <td>Lunch</td>
                            <td>Snack PM</td>
                        </tr>
                    </thead>

                    <?php while ( have_rows('week','option') ) : the_row(); ?>

                        <tr class="menu-row">
                            <td><?php the_sub_field('days','option'); ?></td>
                            <td><?php the_sub_field('snack_am','option'); ?></td>
                            <td><?php the_sub_field('lunch','option'); ?></td>
                            <td><?php the_sub_field('snack_pm','option'); ?></td>
                        </tr>

                    <?php endwhile;?>

                </table>

            <?php endif;?>

        <?php endwhile;?>
        <?php endif; ?>

    <?php
    return ob_get_clean();
}
add_shortcode('menushortcode', 'menu_loop');

推荐阅读