ACF 奇偶中继器


ACF odd even repeater

我有以下代码,我将根据中继器字段是奇数还是偶数显示不同的div 顺序,但它是不正确的,因为它重复偶数两次。有关该问题的任何帮助将不胜感激。

 <!-- If Even -->
<?php if(get_field('services_repeater')): $i = 0; 
while(has_sub_field('services_repeater')): $i++;
if($i % 2 == 0 ):?>
<div class="row">
<div class="span6 area-text">
<?php the_sub_field('area_text'); ?>
</div>
<div class="span6">
<!-- Carousel Open -->
    <div class="slider lazy">
    <?php
    $variable = get_sub_field('choose_slider');
    $args = array(
    'post_type' => 'portfolio',
    'portfolio-item' =>  $variable->slug
    );              
    $the_query = new WP_Query( $args );
    if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div><a class="group1"  href="<?php the_permalink(); ?>" title="<?php printf( the_title_attribute( 'echo=0' ) ); ?>"><div class="image">
    <?php $imageID = get_field('thumbnail');
    $attachment_id = get_field('thumbnail');
        $size = "carousel_main_img";
        $imageURL = wp_get_attachment_image_src( $attachment_id, $size ); ?>
 <img data-lazy="<?php echo $imageURL[0]; ?>" />
 <div class="post-content"><p class="caption"><?php printf( the_title_attribute( 'echo=0' ) ); ?></p></div>
 </div></a></div>
    <?php endwhile; else: ?>
    <?php endif; wp_reset_postdata(); ?>
    </div></div>
<!-- Carousel Closed -->
</div>
</div>
<div id="separator"></div>
<!-- End If Even -->
<?php endif; ?>
<div class="row">
<div class="span6 area-text">
<?php the_sub_field('area_text'); ?>
</div>
<div class="span6">
<!-- Carousel Open -->
    <div class="slider lazy">
    <?php
    $variable = get_sub_field('choose_slider');
    $args = array(
    'post_type' => 'portfolio',
    'portfolio-item' =>  $variable->slug
    );              
    $the_query = new WP_Query( $args );
    if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div><a class="group1"  href="<?php the_permalink(); ?>" title="<?php printf( the_title_attribute( 'echo=0' ) ); ?>"><div class="image">
    <?php $imageID = get_field('thumbnail');
    $attachment_id = get_field('thumbnail');
        $size = "carousel_main_img";
        $imageURL = wp_get_attachment_image_src( $attachment_id, $size ); ?>
 <img data-lazy="<?php echo $imageURL[0]; ?>" />
 <div class="post-content"><p class="caption"><?php printf( the_title_attribute( 'echo=0' ) ); ?></p></div>
 </div></a></div>
    <?php endwhile; else: ?>
    <?php endif; wp_reset_postdata(); ?>
    </div></div>
<!-- Carousel Closed -->
</div>
</div>
<div id="separator"></div>

<?php endwhile; ?>
<?php endif; ?>

恐怕我没有这个在线向您展示,我尝试将其简化为以下代码,但第二个仍然重复两次。

<?php if ( get_field( 'services_repeater' ) ): ?>
  <?php $index = 1; ?>
   <?php $totalNum = count( get_field('services_repeater') ); ?>
    <?php while ( has_sub_field( 'services_repeater' ) ): ?>
    <div class="col-sm-4">
        <?php the_sub_field('area_text'); ?>
    </div>
    <? if ($index % 2 == 0) : ?>
        <? if ($index < $totalNum) : ?>
          Row 2<?php the_sub_field('area_text'); ?>
        <? elseif ($index == $totalNum) : ?>
        <? endif; ?>
    <? endif; ?>
<?php $index++; ?>
<?php endwhile; ?>
<?php endif; ?>

您没有else块。您的代码简而言之如下所示:

if (($i % 2)==0) {
    // do even
}
// do odd.

在偶数的情况下,偶做奇数将会发生。奇数部分必须进入 else 块:

if (($i % 2)==0) {
    // do even
} else {
    // do odd
}

代码中缺少的 else 块是以下行:

<!-- End If Even -->
<?php endif; ?>
// do odd

将其替换为

<?php else: ?>
    // do odd
<?php endif ?>
while( have_rows('custom_section') ): the_row();
if( get_row_index() % 2 == 0 ){
    
    // this is an even row
    
    // code to display the image on the left
    
} else{
    
    // this is an odd row
    
    // code to display the image on the right
    
}

结束;