【问题标题】:PHP - Adding 2 div to a foreach loop every 4 times with ACF RepeaterPHP - 使用 ACF 中继器每 4 次向 foreach 循环添加 2 个 div
【发布时间】:2017-07-28 23:09:06
【问题描述】:

我有一个 ACF 中继器字段,我想将其输出为手风琴网格,如下所示:

<div class="intro row">
  <div class="item item-1">name 1</div>
  <div class="item item-2">name 2</div>
  <div class="item item-3">name 3</div>
  <div class="item item-4">name 4</div>
</div>

<div class="expanded row">      
  <div class="expand" id="item-1">expanded info 1</div>
  <div class="expand" id="item-2">expanded info 2</div>
  <div class="expand" id="item-3">expanded info 3</div>
  <div class="expand" id="item-4">expanded info 4</div>
</div>

<div class="intro row">
  <div class="item item-5">name 5</div>
  <div class="item item-6">name 6</div>
  <div class="item item-7">name 7</div>
  <div class="item item-8">name 8</div>
</div>

<div class="expanded row">      
  <div class="expand" id="item-5">expanded info 5</div>
  <div class="expand" id="item-6">expanded info 6</div>
  <div class="expand" id="item-7">expanded info 7</div>
  <div class="expand" id="item-8">expanded info 8</div>
</div>

我可以很好地对初始行进行分组,这只是我遇到问题的第二个“扩展”行。如何在同一个循环中正确重复和分组 4 的第二行?我当前的 PHP:

<?php // check if the repeater field has rows of data
if( have_rows('features') ):
  // loop through the rows of data

  // add a counter
  $count = 0;
  $group = 0;
  
  while ( have_rows('features') ) : the_row(); 
   
    
	$name = get_sub_field('feature_name');
	$expandedInfo = get_sub_field('feature_info');

    if ($count % 4 == 0) {
      $group++;
      ?>
        <div class="intro row">
      <?php 
    }
    ?>
    <div class="item item-<?php echo $count; ?>">
      <?php echo $name ?>
    </div><!-- item-->
    
   
    <?php 
      if ($count % 4 == 3) {
        ?>
          </div><!-- intro-->
        <?php 
      }
      $count++;
    endwhile;
else :
  // no rows found
endif;

?>

【问题讨论】:

  • if ($count % 2 == 0) 然后添加扩展类!
  • 我不确定它会起作用吗?最初的 4 个必须用不同的类再次重复
  • 你的意思是你想让第一个(第二个 div )类扩展?
  • 如果我们谈论循环的行,它必须重复并分组两次 - 所以 ABCD(intro) ABCD(expand) EFGH(intro) EFGH(expand) 是否有意义?
  • 我不明白这一点,您想获得与您问题中提供的 sn-p 相同的确切结果吗?

标签: php html wordpress advanced-custom-fields


【解决方案1】:

可以完成第二个“扩展”行,以便将每个计数(item-1、item-2)存储在一个数组中,或者在关闭介绍行时遍历所有计数。

<?php
if ($count % 4 == 3) {
    ?>
      </div><!-- intro-->
      <div class="expanded row">
         <?php 
          $start = $count-3; 
          // if $count is 4, $start will be 1, and the $i will go to 4
          // if $count is 8, $start will be 5
         for($i=$start;$i<=$count;$i++){
           echo '<div class="expand" id="item-' . $i . '"></div>';
         } ?>
      </div>
    <?php 
  }

这只是一个例子。我建议您将每个 $count 存储在一个数组中,然后使用 count($array) 来获取它们的数量。遍历数组后,重置它。

数组方法

<?php // check if the repeater field has rows of data
  if( have_rows('features') ):
  // loop through the rows of data

  // add a counter
  $count = 0;
  $group = 0;
  // Content Array
  $content_array = array();
  while ( have_rows('features') ) : the_row(); 


    $name = get_sub_field('feature_name');
    $expandedInfo = get_sub_field('feature_info');
    // Adding the Expanded Info
    $content_array[ 'item-' . $count ] = $expandedInfo;
    if ($count % 4 == 0) {
      $group++;
     ?>
      <div class="intro row">
     <?php 
    }
   ?>
   <div class="item item-<?php echo $count; ?>">
    <?php echo $name ?>
   </div><!-- item-->


   <?php 
     if ($count % 4 == 3) {
    ?>
      </div><!-- intro-->
      <div class="expanded row">
         <?php 

         foreach( $content_array as $item_id => $expanded_info ) {

          echo '<div class="expanded" id="' . $item_id . '">';
          echo $expanded_info;
          echo '</div>';

         } ?>
      </div>
    <?php 
    // Resetting the Array
    $content_array = array();
  }
  $count++;
  endwhile;
else :
// no rows found
endif;

?>

【讨论】:

  • 谢谢,它适用于正确的分组,但是当我以以下格式回显该行的内容时:'
    ' 。 $expandedContent 。 '
    ' 每行只重复最后一行的数据
  • 我已经用数组方法和扩展信息更新了我的答案。看看它是否适合你。请注意在 while 循环之前声明数组。
  • 太棒了,谢谢!
【解决方案2】:

好吧,让我们看看,

在这种情况下,使用变量来存储模板可能会有很大帮助,

如下:

$intro = '';
$expanded = '';
while (have_rows('features')) :

    the_row();

    if ($count % 4 == 0) {
        $group++;

        $intro .= '<div class="intro row">';
        $expanded .= '<div class="expanded row">';

    }
    $intro .= '<div class="item item-' . $count . '"></div><!-- item-->';
    $expanded .= '<div class="expand" id="item-' . $count . '"></div>';

    if ($count % 4 == 3) {
        $intro = '</div><!-- intro-->';
        $expanded = '</div><!-- intro-->';
    }
    $count++;
endwhile;

我已经做了一个简单的例子来解释如何使用变量来解决你的问题:https://3v4l.org/cKPP4

【讨论】:

  • 谢谢,感谢您的努力。我已将变量添加到我原来的 PHP sn-p 中,因为我不确定这是否适用?
  • 你的输出是什么?
  • 直接粘贴您的 sn-p 不会导致任何输出。
  • 您需要echo 变量才能看到输出。
  • 也不输出div
【解决方案3】:

目前无法测试,但我认为类似的方法应该可以。

<?php // check if the repeater field has rows of data
if( have_rows('features') ):
  // loop through the rows of data

  // add a counter
  $count = 0;
  $group = 0;
  $array = array();

  while ( have_rows('features') ) : the_row(); 

    $name = get_sub_field('feature_name');
    $expandedInfo = get_sub_field('feature_info');
    if ($count % 4 == 0) {
      $group++;
      ?>
        <div class="intro row">
      <?php 
    }
    ?>
    <div class="item item-<?php echo $count; ?>">
      <?php echo $name ?>
    </div><!-- item-->


    <?php 
        array_push($array, $expandedInfo);
      if ($count % 4 == 3) {
        ?>
          </div><!-- intro-->
          <div class="expanded row">
        <?php
        for ($i=0; $i < count($array); $i++) { 
            echo '<div class="expand" id="item-'.$i + 1.'">'.$array[$i].'</div>';
        }
        echo '</div>';
      }
      $count++;
    endwhile;
else :
  // no rows found
endif;

?>

【讨论】:

  • 谢谢,它适用于正确的分组,但是当我以以下格式回显行的内容时:'
    ' . $展开内容。 '
    ' 每组只重复最后一行的数据
  • 那么你应该把每一行的$expandContent$count一起存起来。
  • 仔细检查后,扩展 ID 计数似乎存在问题,item-'.$i + 1.'
  • 你能描述我的问题吗?
  • 似乎又是在呼应展开的内容而不是计数
猜你喜欢
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 2016-08-19
相关资源
最近更新 更多