【问题标题】:Adding div around every 2 repeater fields ACF在每 2 个中继器字段 ACF 周围添加 div
【发布时间】:2017-05-17 14:03:23
【问题描述】:

我需要在输入的每对 ACF 转发器字段周围添加一个 div。 我有这个代码的 sn-p,它为每三个字段完成这项工作。每次我更改数字时,它都会使下一个 div ID 成为前一个的子 ID。 任何建议都会很棒!谢谢

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

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

  while ( have_rows('services') ) : the_row(); 
    // vars
    $teacher_bio = get_sub_field('service_title');
    $teacher_name = get_sub_field('service_information');
    $teacher_image = get_sub_field('icon');
    if ($count % 3 == 0) {
      $group++;
      ?>
        <div id="services-<?php echo $group; ?>" class="cf group-<?php echo $group; ?>">
      <?php 
    }
    ?>
    <div class="service">
      <img src="<?php the_sub_field('icon'); ?>" />
      <p><?php echo $teacher_name; ?></p>
      <?php echo $teacher_bio; ?>
    </div><!-- .teacher -->
    <?php 
      if ($count % 3 == 2) {
        ?>
          </div><!-- #services -->
        <?php 
      }
      $count++;
    endwhile;
else :
  // no rows found
endif;

【问题讨论】:

  • 如果您的中继器中只有一个中继器,这不是更简单吗?那么每个“教师/服务”都会有一行,您可以将其包装在您想要的任何 html 中

标签: wordpress advanced-custom-fields


【解决方案1】:

您可以使用自定义 jquery 轻松完成此操作。

【讨论】:

    【解决方案2】:

    你的模数函数不正确。使用% 2,因为您想每两个循环检查一次。在起始 div 处使用 0,在结束 div 处使用 1

    我为你创建了一个简单的例子:

    <?php
    
    $arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    $count = 0;
    
    foreach ($arr as &$value) {
    
        if ($count % 2 == 0) {
            echo '<div>';
        }
    
        echo $value;
    
    
        if ($count % 2 == 1) {
            echo '</div>';
        }
    
        $count++;
    
    }
    

    结果: &lt;div&gt;12&lt;/div&gt;&lt;div&gt;34&lt;/div&gt;&lt;div&gt;56&lt;/div&gt;&lt;div&gt;78&lt;/div&gt;&lt;div&gt;910&lt;/div&gt;

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 2019-03-30
      • 2017-01-01
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      相关资源
      最近更新 更多