2014-11-23 91 views
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; ?> 
+0

嘿,你的思想就使用模运算符来检测奇数/偶数是完全正确的。 你能告诉我们一些示例输出的问题发生的地方,因为从我能读的代码看起来不错。 – 2014-11-25 18:54:40

回答

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 ?> 
+0

我正盯着它呢!谢谢你的帮助。非常感激! – Hue 2014-11-26 14:22:08