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