简体   繁体   中英

Output Nested ACF Repeater Field as Table in Shortcode

I'm trying to display nested ACF Fields in a table through a shortcode on an elementor page.

I've tried merging a shortcode with some code that I found on google however I wasn't successful.

The repeater part was taken from this link: https://support.advancedcustomfields.com/forums/topic/help-with-creating-a-table-using-nested-repeaters/

When I use the shortcode there is nothing.

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

PARTLY RESOLVED.. doing more research/digging I noticed that since these settings are coming the ACF options page we need to add a 'option' to some parts of the code here is the working code

Now that I have everything display correctly is there a way to display each "Week" as its own tab with it's own corresponding table?


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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