首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每次post循环后重置计数器

每次post循环后重置计数器
EN

Stack Overflow用户
提问于 2019-11-28 23:48:28
回答 1查看 189关注 0票数 1

我在一个自定义post类型的循环中有一个ACF中继器,它有一个计数器,每隔两个col 6就会产生一行。当我有偶数个col时,这很好用,但当它不均匀时就不好用了。当一篇文章有一个偶数个列时,计数器会在下一篇文章中以某种方式记住这一点,并且只在第一行显示一个列。

在这里你会找到当前的代码和正在发生的事情的一个小图片。不知怎么的,我需要在每次post循环后重置计数器,但我找不到答案。wp_reset_postdata似乎不起作用。

代码语言:javascript
复制
<?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; ?>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-29 14:44:57

只是一个小小的改变。您需要确保在acf循环开始之前将计数器重置为0。

代码语言:javascript
复制
        <?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; ?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59092400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档