0

I want to create a shortcode using ACF Repeater Field and so I've found this code and when I tried to apply it on my website it doesn't work. Am using genesis frame work.

my aim is create a shortcode using ACF Repeater Field and display table in post or page.

Here is my code in functions.php:

    function menu_loop () { 

    echo    '<div class="entry-content dishes">';
// check if the repeater field has rows of data
        if( have_rows('menu_sections') ):

            // loop through the rows of data
            while ( have_rows('menu_sections') ) : the_row();

                // display a sub field value

                echo '<h2>' . get_sub_field('section_title') . '</h2>';
                if ( have_rows('sections_items'));?>

                    <table>

                        <thead>
                            <tr>
                                <td class="ja_name">Name</td>
                                <td class="ja_description">Description</td>
                                <td class="ja_price">Price</td>
                            </tr>
                        </thead>  

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

                        <tr>
                            <td><?php the_sub_field('dish_names'); ?></td>
                            <td><?php the_sub_field('dish_description'); ?></td>
                            <td>$ <?php the_sub_field('dish_price'); ?></td>
                        </tr>

                    <?php endwhile;?>

                    </table> <?php 

            endwhile;

         else : 

            // no rows found

        endif; ?></div>
add_shortcode('testimonials', 'menu_loop');
3
  • @toscho, this question was actually the original. the other one that is on hold was the duplicate. can you change?
    – rudtek
    Dec 16, 2016 at 17:37
  • They are all equally off topic. I don't see why they need special care.
    – fuxia
    Dec 16, 2016 at 19:55
  • @toscho this is the original post. please remove duplicate
    – raja
    Dec 17, 2016 at 13:10

1 Answer 1

2

you need to return your code when using shortcodes. Additionally you didn't close the function.

try this:

function menu_loop() {

    $menu = '<div class="entry-content dishes">';

    // check if the repeater field has rows of data
    if (have_rows('menu_sections')):

        // loop through the rows of data
        while (have_rows('menu_sections')) : the_row();

            // display a sub field value

            $menu .= '<h2>' . get_sub_field('section_title') . '</h2>';
            if (have_rows('sections_items')) :

                $menu .= '<table><thead><tr><td class="ja_name">Name</td><td class="ja_description">Description</td><td class="ja_price">Price</td></tr></thead>';

                while (have_rows('section_items')): the_row();
                    $menu .= '<tr><td>' . the_sub_field('dish_names') . '</td><td>' . the_sub_field('dish_description') . '</td><td>$ ' . the_sub_field('dish_price') . '</td></tr>';
                endwhile;

                $menu .= '</table> ';

            else :
                // no rows found
            endif;

        endwhile;

    else :
        //echo 'no rows found';
    endif;

    $menu .= '</div>';
    // Code
    return $menu;
}

add_shortcode('testimonials', 'menu_loop');

I just checked your code one more time... you are echoing the_sub_field on your while statement. That might mess things up. Try changing the_sub_field to get_sub_field in your while statement (x3 issues) if the above code places your fields out of place.

10
  • shows syntax error, unexpected '$menu' (T_VARIABLE), expecting ';'
    – raja
    Dec 15, 2016 at 9:41
  • oops. missed one of the php closures. 2 lines up from // code. I'll be at a computer a bit later and test if that doesn't work
    – rudtek
    Dec 15, 2016 at 16:27
  • please help me about this code. i found endwhile missed ;. i think
    – raja
    Dec 15, 2016 at 18:10
  • shows Parse error: syntax error, unexpected end of file
    – raja
    Dec 15, 2016 at 18:31
  • give me a couple minutes
    – rudtek
    Dec 15, 2016 at 18:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.