我在一个自定义post类型的循环中有一个ACF中继器,它有一个计数器,每隔两个col 6就会产生一行。当我有偶数个col时,这很好用,但当它不均匀时就不好用了。当一篇文章有一个偶数个列时,计数器会在下一篇文章中以某种方式记住这一点,并且只在第一行显示一个列。
在这里你会找到当前的代码和正在发生的事情的一个小图片。不知怎么的,我需要在每次post循环后重置计数器,但我找不到答案。wp_reset_postdata似乎不起作用。
<?php $args = array( 'post_type' => 'posttypename', 'posts_per_page' => '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1><?php the_title(); ?></h1>
</div>
</div>
<?php if( have_rows('row') ): ?>
<div class="row">
<?php while( have_rows('row') ): the_row(); $text = get_sub_field('text'); ?>
<div class="col-md-6">
<?php echo $text; ?>
</div>
<?php $counter++; if($counter % 2 === 0) : echo '</div> <div class="row">'; endif; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

发布于 2019-11-29 14:44:57
只是一个小小的改变。您需要确保在acf循环开始之前将计数器重置为0。
<?php $counter = 0; //Initialize your Counter here before the Loop Starts for each Post ?>
<?php if( have_rows('row') ): ?>
<div class="row">
<?php while( have_rows('row') ): the_row(); $text = get_sub_field('text'); ?>
<div class="col-md-6">
<?php echo $text; ?>
</div>
<?php $counter++; if($counter % 2 === 0) : echo '</div> <div class="row">'; endif; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>https://stackoverflow.com/questions/59092400
复制相似问题