2
我有以下代码,我将根据中继器字段是奇数还是偶数来显示不同的div顺序,但由于它重复两次,所以它不正确。对这个问题的任何帮助将不胜感激。ACF奇数中继器
<!-- 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; ?>
嘿,你的思想就使用模运算符来检测奇数/偶数是完全正确的。 你能告诉我们一些示例输出的问题发生的地方,因为从我能读的代码看起来不错。 – 2014-11-25 18:54:40