【问题标题】:foreach statement within ACF repeaterACF 转发器中的 foreach 语句
【发布时间】:2018-05-09 05:02:31
【问题描述】:

我不确定我这样做是否正确?我有一个嵌套的 ACF 中继器,它工作正常,但该部分都显示为一个单独的块。我希望能够将块分成列。所以我想这样做我需要一个“foreach”语句,这样我就可以为每个部分分配一个 id 或类,让我能够单独设置每个部分的样式 - 浮动和添加边距等。

我不能让它工作!

<div class="col-xl-12 food-section">


<?php
$rows = get_field('section_container');
if($rows) {
foreach($rows as $row):
?>

<div class="col-xl-6 section-test"> <?php
    if( have_rows('section_container') ) :  
    while( have_rows('section_container') ): the_row(); ?>

<h1><?php the_sub_field('section_heading'); ?></h1>
        <div class="section-image"><?php
            $image = get_sub_field('section_image');
            if( !empty($image) ): ?>
                <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
        </div>

        <?php endif;

            if( have_rows('sub_section_container') ): 
            while( have_rows('sub_section_container') ): the_row();
            ?>
                <h2><?php the_sub_field('sub_section_heading'); ?></h2>
                <?php
                    if( have_rows('food_item') ): 
                    while( have_rows('food_item') ): the_row();
                ?>

<div id="menu-table">
    <h3 id="leftside"><?php the_sub_field('item_name'); ?></h3>
    <h4 id="rightside"><?php the_sub_field('price'); ?></h4>
    <div id="menu-description"><p><?php the_sub_field('item_description'); ?></p></div>
    <div class="stroke"></div>
</div>

                <?php   endwhile; //food_item
                    endif;  //food_item

            endwhile; //section_h1_container
            endif;  //section_h1_container

    endwhile; //section_container
    endif;  //section_container

    wp_reset_postdata();

    endforeach; }
?></div>

    </div><!-- food-section -->

【问题讨论】:

  • 添加php标签可能会获得更多浏览量
  • “while(have_rows('section_container')): the_row();”与您的 foreach 执行相同的工作,并且“if( have_rows('section_container') ) :”与您的 foreach 上方的 if 语句执行相同的工作。看看这里阅读嵌套中继器:advancedcustomfields.com/resources/…

标签: php arrays foreach repeater advanced-custom-fields


【解决方案1】:

我已经清理了你的代码,希望我能理解你的要求。您在 sn-p 的顶部有一个不必要的 if 和 foreach,以及在 ACF 中的中继器调用结束时不需要的 wp_reset_postdata()。

您可以在此处阅读有关嵌套中继器的更多信息:

https://www.advancedcustomfields.com/resources/working-with-nested-repeaters/

<?php if( have_rows('section_container') ) : ?>
 <div class="col-xl-12 food-section"> 
      <?php while( have_rows('section_container') ): the_row(); ?>
           <div class="col-xl-6 section-test">
                <h1><?php the_sub_field('section_heading'); ?></h1>
                <div class="section-image">
                     <?php $image = get_sub_field('section_image');
                     if( !empty($image) ): ?>
                          <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
                     <?php endif; ?>
                </div>
                <?php if( have_rows('sub_section_container') ): 
                     while( have_rows('sub_section_container') ): the_row(); ?>
                          <h2><?php the_sub_field('sub_section_heading'); ?></h2>
                          <?php if( have_rows('food_item') ): 
                               while( have_rows('food_item') ): the_row(); ?>
                                    <div id="menu-table">
                                        <h3 id="leftside"><?php the_sub_field('item_name'); ?></h3>
                                        <h4 id="rightside"><?php the_sub_field('price'); ?></h4>
                                        <div id="menu-description"><p><?php the_sub_field('item_description'); ?></p></div>
                                        <div class="stroke"></div>
                                    </div>
                               <?php   endwhile; //food_item
                          endif;  //food_item
                     endwhile; //section_h1_container
                endif;  //section_h1_container ?>
           </div>
      <?php endwhile; //section_container ?>
 </div>
<?php endif;  //section_container ?>

这个 sn-p 检查是否为“section_container”设置了任何行,如果是,它会创建“food-section” div。然后它循环遍历“section_container”的每一行。对于每一行,它都会创建一个“section-test”div,并开始使用该行的子字段数据填充它。然后它会检查当前行内的“sub_section_container”中继器,并循环遍历嵌套中继器内的行,并输出数据。

【讨论】:

    猜你喜欢
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 2017-09-27
    • 2021-04-06
    • 2018-10-06
    • 1970-01-01
    • 2019-05-24
    相关资源
    最近更新 更多