【发布时间】:2015-11-29 12:48:57
【问题描述】:
抱歉标题,我不知道如何措辞。
基本上,我有一个网页,它使用高级自定义字段来填充产品,它遍历产品并以每行三个一组的形式显示它们;每行颜色交替变化 - 浅灰色到深灰色。
我的问题是,我编写的代码是检查每行是否有 3 个项目,因此如果产品列表行以 1 或 2 结束,则包含 div 的行不会关闭,从而包裹我的页脚并弄乱 for页面的其余部分。以下是我的代码,应该很容易理解 - 你也可以看到我正在使用 LazyLoadAny - 基本上,我计算项目,如果超过 6 个,我会延迟加载其余的。在第一个项目上我打开行容器,当有三个时我关闭它。再说一次,我需要弄清楚如果该行以 1 或 2 个项目而不只是 3 个项目结束,如何关闭容器。
短版:如果“internal-product-light”或“internal-product-dark”行以 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>
【问题讨论】:
-
最好先把你做过的事情从头开始,从
array_chunk()开始 -
谢谢@Popnoodles,你能给我举个例子吗?我会研究 array_chunk() 但一个样本将不胜感激。
-
您将项目分块成行(共 3 行),然后迭代这些行(然后是其中的每个项目),因此您只需计算迭代时行中有多少项目。只有最后一行可能少于三个,但无论如何您都在关闭该行。
-
我投票结束这个问题作为离题,因为用户解决了这个问题,它不太可能对其他人有用。
标签: php arrays while-loop lazy-loading advanced-custom-fields