Support

Account

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

  • 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
    }