2016-12-13 311 views
0

基本上我想要做的是通過中繼器字段(ACF,在WordPress中工作)循環選取1個子字段,並在第一項在循環中運行第二個循環來拾取另一個子字段。循環內的PHP循環(每個循環的每次迭代)

  • 循環1開始
    • 項目1,場1個
    • 項目2,場1個
    • 項目3,字段1
  • 迴路1停止
  • 迴路2點開始
    • 項1,字段2
    • 項目2,場2
    • 項目3,字段2
  • 迴路2停止
  • 迴路1重新啓動
    • 項目4,字段1
    • 項目5,字段1
    • 項目6,字段1
  • 迴路1停止
  • 迴路2重新啓動
    • 項目4,場2
    • 項目5,場2
    • 項目6,場2
  • 迴路2停止

例如,如果我有一個具有兩個子字段「title」和「content」的中繼器,輸出將如下所示:

<div class="row"> 

    <div class="column"> 
    <div class="tabs-title"> 
     <h3>Item 1 Title</h3> 
     <a href="#panel1">Learn More</a> 
    </div> 
    </div> 
    <div class="column"> 
    <div class="tabs-title"> 
     <h3>Item 2 Title</h3> 
     <a href="#panel2">Learn More</a> 
    </div> 
    </div> 
    <div class="column"> 
    <div class="tabs-title"> 
     <h3>Item 3 Title</h3> 
     <a href="#panel3">Learn More</a> 
    </div> 
    </div> 

    <div class="tabs-panel" id="panel1"> 
    <div class="tabs-content"> 
     <p>Item 1 content goes here.</p> 
    </div> 
    </div> 
    <div class="tabs-panel" id="panel2"> 
    <div class="tabs-content"> 
     <p>Item 2 content goes here.</p> 
    </div> 
    </div> 
    <div class="tabs-panel" id="panel3"> 
    <div class="tabs-content"> 
     <p>Item 3 content goes here.</p> 
    </div> 
    </div> 

</div> 

<!-- Repeat loop until the end --> 

到目前爲止,這就是我所擁有的。只是不知道如何執行第二個循環,然後每隔三個選項關閉div。

<?php if(have_rows('services')) : 
    $i = 1; 
    $divopen = '<div class="cta row row-3 small-up-1 large-up-3 tabs" data-tabs id="example-tabs">'; 
    echo $divopen; 
    while(have_rows('services')) : the_row(); ?> 

    <div class="column"> 
     <div class="tabs-title"> 
     <h3><?php the_sub_field('service_name'); ?></h3> 
     <a href="#panel<?php echo $i; ?>">Learn More</a> 
     </div> 
    </div> 

    <?php if($i % 3 == 0) : 
     echo '</div>' . $divopen; 
    endif; 

    $i++; endwhile; echo '</div>'; 
endif; ?> 

回答

0

希望你能找到這個解決方案正確的,你可以通過一個數組循環,創造這樣的表結構(這個答案是根據我的理解):

$array = [ 
    [ 
     [ 
      'title' => "My Title 1", 
      'content' => 'Content 1', 
     ], 
     [ 
      'title' => "My Title 2", 
      'content' => 'Content 2', 
     ], 
     [ 
      'title' => "My Title 3", 
      'content' => 'Content 3', 
     ], 
    ], 
    [ 
     [ 
      'title' => "My Title 4", 
      'content' => 'Content 4', 
     ], 
     [ 
      'title' => "My Title 5", 
      'content' => 'Content 5', 
     ], 
    ], 
]; 

echo "<table style='border: 1px solid black;'><tbody>"; 
foreach ($array as $key => $arr) { 
    echo "<tr> . '<br>'"; 
    foreach ($arr as $key => $value) { 
     echo "<td style='border: 1px solid black;'>"; 
     echo $value['title']; 
     echo $value['content']; 
     echo '</td>'; 
    } 
    echo "<tr>"; 
} 
echo "</tbody></table>"; 

希望這幫助!