ACF奇数甚至中继器

时间:2014-11-23 15:15:09

标签: php advanced-custom-fields

我有以下代码,其中我将显示不同的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; ?>

1 个答案:

答案 0 :(得分:3)

您没有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 ?>