PHP产品列表-循环结束结束行

PHP产品列表-循环结束结束行,php,arrays,while-loop,lazy-loading,advanced-custom-fields,Php,Arrays,While Loop,Lazy Loading,Advanced Custom Fields,对不起,这个标题,我不知道该怎么说 基本上,我有一个使用高级自定义字段填充产品的网页,它在产品中循环并以每行三人一组的方式显示它们;每行交替颜色-浅灰色到深灰色 我的问题是,我编写的代码正在检查每一行是否有3个项目,因此,如果产品列表行以1或2结尾,则包含div的行不会关闭,从而将我的页脚包装起来,并将页面的其余部分弄乱。下面是我的代码,应该很容易理解-你也可以看到我在使用LazyLoadAny-基本上,我会计算项目,如果有超过6个,我会lazyload其余的。在第一个项目中,我打开行容器,当有

对不起,这个标题,我不知道该怎么说

基本上,我有一个使用高级自定义字段填充产品的网页,它在产品中循环并以每行三人一组的方式显示它们;每行交替颜色-浅灰色到深灰色

我的问题是,我编写的代码正在检查每一行是否有3个项目,因此,如果产品列表行以1或2结尾,则包含div的行不会关闭,从而将我的页脚包装起来,并将页面的其余部分弄乱。下面是我的代码,应该很容易理解-你也可以看到我在使用LazyLoadAny-基本上,我会计算项目,如果有超过6个,我会lazyload其余的。在第一个项目中,我打开行容器,当有三个时,我关闭它。同样,我需要弄清楚如果行以1或2项结束,而不仅仅是3项结束,如何关闭容器

简短版本:如果“内部产品亮”或“内部产品暗”行以1或2个产品而不是3个产品结尾,如何关闭它们

<?php
$count = 0;
$rowCount = 0;
$rowClass = "internal-product-light";
// check if the repeater field has rows of data
if( have_rows('product_listing_repeater') ): ?>
<div class="internal-container">
    <div class="full-product-line-contain">
        <?php // loop through the rows of data
        while ( have_rows('product_listing_repeater') ) : the_row(); $count++; $rowCount++; ?>
            <?php if($rowCount  == 1) { ?>
                <div class="<?php echo $rowClass; ?>">
                <div class="product-centering">

                <?php if($count > 6) { ?>
                    <div class="js-lazyload">
                    <?php echo "<!--"; }
            } ?>
            <div class="internal-section product-line-item product-alignment clearfix">
                <div class="product-contain">
                    <?php if(get_sub_field('has_link')): ?>
                        <a href="<?php the_sub_field('link_url')?>" class="product-link">
                            <?php
                            if(get_sub_field('has_image')):
                                $singleImage = get_sub_field('block_image');
                                ?>
                                <img class="product-line-image" src="<?php echo $singleImage ?>" />
                            <?php endif; ?>
                        </a>
                    <?php endif; ?>
                    <div class="internal-content-contain-right no-float">
                        <?php if(get_sub_field('has_link')): ?>
                            <a href="<?php the_sub_field('link_url')?>" class="product-link-title">
                                <h2><?php the_sub_field('block_headline'); ?></h2>
                            </a>
                        <?php endif; ?>
                        <?php if(get_sub_field('has_link')): ?>
                            <a href="<?php the_sub_field('link_url')?>" class="product-line-btn"><?php the_sub_field('link_text')?></a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <?php if($rowCount == 3) { ?>
                <?php if($count > 6) { echo "-->"; ?>
                    </div><!-- End lazy load div -->
                <?php } ?>
                </div><!-- End product centering -->
                </div><!-- End row -->
                <?php $rowCount = 0;
                if($rowClass == "internal-product-light"){ $rowClass = "internal-product-dark";}else{$rowClass = "internal-product-light";}
            } ?>
        <?php endwhile; ?>
    </div>
</div>


您的最后一个if陈述可能是:

<?php if(($rowCount % 3) == 0) { ?>
    <?php if($count > 6) { echo "-->"; ?>
    </div><!-- End lazy load div -->
    <?php } ?>
    </div><!-- End product centering -->
    </div><!-- End row -->
    <?php if($rowClass == "internal-product-light"){ $rowClass = "internal-product-dark";}else{$rowClass = "internal-product-light";}
    } ?>

然后,在while块之后:

<?php if(($rowCount % 3) <> 0) { ?>
    <?php if($count > 6) { echo "-->"; ?>
    </div><!-- End lazy load div -->
    <?php } ?>
    </div><!-- End product centering -->
    </div><!-- End row -->
    <?php if($rowClass == "internal-product-light"){ $rowClass = "internal-product-dark";}else{$rowClass = "internal-product-light";}
    } ?>


x%y
返回
x/y

的其余部分,因此下面的代码实际上是一个比我想象的简单得多的解决方案-我将发布代码,然后将其分解

<?php
$count = 0;
$rowCount = 0;
$rowClass = "internal-product-light";
$rowInfo = get_sub_field('product_listing_repeater');
$fieldCount = count($rowInfo);
// check if the repeater field has rows of data
if( have_rows('product_listing_repeater') ): ?>
<div class="internal-container">
    <div class="full-product-line-contain">
        <?php // loop through the rows of data
        while ( have_rows('product_listing_repeater') ) : the_row(); $count++; $rowCount++; ?>
            <?php
               if($rowCount  == 1) { ?>
                <div class="<?php echo $rowClass; ?>">
                <div class="product-centering">

                <?php if($count > 6) { ?>
                    <div class="js-lazyload">
                    <?php echo "<!--"; }
            } ?>
            <div class="internal-section product-line-item product-alignment clearfix">
                <div class="product-contain">
                    <?php if(get_sub_field('has_link')): ?>
                        <a href="<?php the_sub_field('link_url')?>" class="product-link">
                            <?php
                            if(get_sub_field('has_image')):
                                $singleImage = get_sub_field('block_image');
                                ?>
                                <img class="product-line-image" src="<?php echo $singleImage ?>" />
                            <?php endif; ?>
                        </a>
                    <?php endif; ?>
                    <div class="internal-content-contain-right no-float">
                        <?php if(get_sub_field('has_link')): ?>
                            <a href="<?php the_sub_field('link_url')?>" class="product-link-title">
                                <h2><?php the_sub_field('block_headline'); ?></h2>
                            </a>
                        <?php endif; ?>
                        <?php if(get_sub_field('has_link')): ?>
                            <a href="<?php the_sub_field('link_url')?>" class="product-line-btn"><?php the_sub_field('link_text')?></a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <?php if($rowCount == 3 || $count == $fieldCount) { ?>
                <?php if($count > 6 || $count == $fieldCount) { echo "-->"; ?>
                    </div><!-- End lazy load div -->
                <?php } ?>
                </div><!-- End product centering -->
                </div><!-- End row -->
                <?php $rowCount = 0;
                if($rowClass == "internal-product-light"){ $rowClass = "internal-product-dark";}else{$rowClass = "internal-product-light";}
            } ?>
        <?php endwhile; ?>
    </div>
</div>


最好是从头开始,从
array\u chunk()
Thank you@poplaines开始,你能给我举个例子吗?我将研究array_chunk(),但如果能提供一个示例,我将不胜感激。您可以将项目分为几行(共3行),然后迭代这些行(然后迭代其中的每个项目),因此您只需在迭代时计算该行中的项目数。只有最后一行可能少于三行,但在任何情况下,您都将关闭该行。我投票将此问题作为非主题关闭,因为用户解决了该问题,并且它不太可能对其他任何人有用。我现在看到,您可以在
if($rowCount%3)0中省略
if($rowClass…
-station)
,因为这似乎只在while语句中使用。不过,我希望你能理解这一点。谢谢你的回答。让我试一试,试着让它运行起来。感谢你的帮助和时间-如果我让它运行起来,我会标记你的答案,否则我会提供反馈。这肯定很接近,不是100%,但让我更接近一个so谢谢。在我完全弄清楚之前,我会投票,但不会选择作为答案。如果我让它工作起来,我也会发布一份编辑过的副本。问题出在哪里?你是否将while语句的第一行从
更改为
?这是一件事吗?我想这对后面的人很有用,这就是我回答的原因“我知道你在这里比我有更多的代表和时间,所以我会尽我所能去做最好的事情。”Popper说