Support

Account

Home Forums Add-ons Repeater Field Output repeater field as table

Solving

Output repeater field as table

  • Hi,

    I’ve used ACF at a basic level for a couple of years, but I’m now finding I need to dig a bit deeper.

    The site I’m working needs to publish a catalogue of products. I’ve set-up a repeater field (product) and want to output the contents in a table format. It will display Product Name (product_name), Product Description (product_description), Product Price (product_price) and Product Notes (product_notes).

    I also need to split these products into categories (product_category) to make navigation easier.

    And finally, I’ll will add the info to pages as a shortcode.

    I’ve made a start, but without any php experience, I’m really struggling and would appreciate any help:

    <?php if( have_rows('product') ): ?>
    
            while ( have_rows('product') ) : the_row(); ?>
    
                <h2><?php the_sub_field(‘product_category'); ?></h2>
    
                <?php if( have_rows('product_category') ): ?>
    
                    <table>
    
                        <thead>
                            <tr>
                                <td>Product</td>
                                <td>Description</td>
                                <td>Price</td>
                                <td>Notes</td>
                            </tr>
                        </thead>
    
                        <?php while ( have_rows('product_category') ) : the_row(); ?>
    
                            <tr>
                                <td><?php the_sub_field(‘product_name'); ?></td>
                                <td><?php the_sub_field(‘product_description'); ?></td>
                                <td><?php the_sub_field(‘product_price'); ?></td>
                                <td><?php the_sub_field(‘product_notes’); ?></td>
                            </tr>
    
                        <?php endwhile; ?>
    
                    </table>
    
                <?php endif; ?>
            <?php endwhile; ?>
     <?php endif; ?>
  • Hi,

    Just wondering if anyone can help me resolve this problem, thank you.

  • With something like this you’d really be better off using something like a custom post type for products and a custom taxonomy for product category. If you’re going to have a lot of products and categories then trying use a repeater will eventually get out of hand and will prevent you from having flexibility in the future to make improvements.

    That being said…

    Is “product_category” a repeater or some other type of field? You have called twice

    
    <h2><?php the_sub_field('product_category'); ?></h2>
    
    <?php if( have_rows('product_category') ): ?>
    

    Other than that I don’t see anything in your code that is a problem except the use of smart quotes ‘product_notes’ which will cause a PHP error.
    The first indicates a field like a text field an the second is using it as a repeater.

  • Hi John,

    Thank you for your reply, that’s really helpful.

    “product_category” is a field within a repeater field group, the attached screenshot shows the hierarchy. The idea is to have the products listed/arranged by Product Category to make items easier to find.

    Thank you too for the smart quote tip. I’ll convert any to neutral (vertical) characters.

    I think you’re right about creating a custom post type. That’s the way I was originally going to tackle it and will go back to that as the requested categories seem to be growing by the day! As you can see, the problem I always have is getting content to render in a table – it seems to be quite difficult without good php knowledge.

  • …as an follow-up, I’ve changed the quotes (thank you), but getting a validation error on the second last line <?php endwhile; ?>

    Syntax errors detected :
    syntax error, unexpected ‘endwhile’ (T_ENDWHILE), expecting end of file (Line 35)

    It seems to close the while loop on the second line, so not sure what’s causing the problem.

  • In order to display the repeater by category you would first need to sort the repeater by a sub field.

    There is some documentation here on using array_multisort() https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/ (PHP Doc https://www.php.net/manual/en/function.array-multisort.php)

    If you know how to do so you might also want to look into using usort() which is the method I prefer https://www.php.net/manual/en/function.usort.php. Sorry, I don’t have time at the moment to give you an example, but I did post an example of it here https://support.advancedcustomfields.com/forums/topic/sorting-repeater-fields-by-date/ this can probably be used in an acf/load_value filter like the ACF example.

    Once you have the repeater sorted then you need to loop over the repeater, keep track of the current category, previous category and then start a new output when the category changes.

    
    $prev_category = '';
    $table_started = false;
    while (have_rows('product')) {
      $this_category = get_sub_field('product_category');
      if ($this_category != $prev_category) {
        // category switch
        if ($table_started) {
          // close previous table elements
          $table_started = false;
        }
        // output heading for new category
      }
      if (!$table_started) {
        // output opening elements of new table
        $table_started = true;
      }
      // output table row for product
    }
    
  • Hi John,

    That’s great, thank you for all your help. I’ll look into the links and example you gave and see if I can complete the task – it’s all quite a steep learning curve!

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.