duanmiexi2275 2019-05-16 07:54
浏览 41
已采纳

第n行ACF REPEATER的不同布局

I am using ACF repeater to display images, I want to achieve layout so that 1 - 2 - 3 elements go with col-lg-4 grid and 4-5-6-7 go with col-lg-3 grid and so on, repeating that layout for all items

I've tried using but it's to get 3 elements into 1 div

mostly my layout will be

first row 3x col-lg-4
second row 4x col-lg-3
third row 3x col-lg-4
fourth row 4x col-lg-3 


<?php 

// check if the repeater field has rows of data
if( have_rows('gallery_repeater') ):
    // loop through the rows of data

    // add a counter
    $count = 0;
    $group = 0;

    while ( have_rows('gallery_repeater') ) : the_row(); 
      // vars
      $teacher_bio = get_sub_field('image');
      if ($count % 3 == 0) {
        $group++;
        ?>
          <div id="teachers-<?php echo $group; ?>" class="cf group-<?php echo $group; ?>">
        <?php 
      }
      ?>
      <div class="teacher">
        <img src="<?php the_sub_field('image'); ?>" />

        <?php echo $teacher_bio; ?>
      </div><!-- .teacher -->
      <?php 
        if ($count % 3 == 2) {
          ?>
            </div><!-- #teachers -->
          <?php 
        }
        $count++;
      endwhile;
  else :
    // no rows found
  endif;

?>
  • 写回答

1条回答 默认 最新

  • douxu2081 2019-05-16 09:44
    关注

    Hi Please check below code for reference :

    $gallery_repeater = get_field('gallery_repeater');
    foreach (array_chunk($gallery_repeater, 7) as $key => $value) {
        foreach ($value as $k => $val) {
            $class = $k < 3 ? 'col-lg-3' : 'col-lg-4';
            echo '<div class="'.$class.'"> <img src="'.$val['image'].'" />'.$val['teacher_bio'].'</div>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站