Support

Account

Home Forums Add-ons Repeater Field Repeater field with IF/ELSE based on sub_field value Reply To: Repeater field with IF/ELSE based on sub_field value

  • The first thing I would do is simplify the code. You don’t need all those if/elseif statements and you can reduce the number of post loops to 1 using the values from the sub fields.

    But you will need to build a card template part for the case studies

    
    <?php 
      
      if (have_rows('row_order_and_qty')) {
        while(have_rows('row_order_and_qty')) {
          the_row();
          $post_type = get_sub_field('content_type');
          $posts_per_page = get_sub_field('items_per_row');
          $row_query = new WP_Query(array(
            'post_type' => $post_type,
            'post_status' => 'publish',
            'posts_per_page' => $posts_per_page,
            'orderby' => 'date',
            'order' => 'DESC'
          ));
          if ($row_query->have_posts()) {
            switch($post_type) {
              case 'auction':
                $icon = '<i class="fas fa-gavel"></i>';
                $heading = 'Auctions';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/auctions';
                break;
              case 'liquidation':
                $icon = '<i class="fas fa-file-invoice-dollar"></i>';
                $heading = 'Liquidations';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/past-auctions';
                break;
              case 'inventory':
                $icon = '<i class="fas fa-award">';
                $heading = 'Featured Inventory';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/inventory';
                break;
              case 'studies':
                $icon = '<i class="fas fa-file-invoice"></i>';
                $heading = 'Case Studies';
                $class = 'row row-cols-1 row-cols-md-2 row-cols-lg-3';
                $link_url = '/studies';
                break;
            } // end switch
            ?>
              <h2 class="section-title mt-5"><?php 
                echo $icon; ?> <?php 
                echo $heading; ?> 
                <a href="<?php 
                  echo $link_url; ?>" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
              <div class="<?php echo $class; ?>">
                <?php 
                  while ($row_query->have_posts()) {
                    $row_query->the_post();
                    ?>
                      <div class="col mb-4">
                        <?php get_template_part('template-parts/card', $type); ?>
                      </div>
                    <?php 
                  } // end while have posts
                  wp_reset_postdata();
                ?>
              </div>
            <?php 
          } // end if have posts
        } // end while have rows
      } // end if have rows
    ?>