Magento 1 add div inside list foreach
-
31-12-2020 - |
Question
I want to add a specific div after every 3 items inside the category list. So I want to extend the code of /app/design/frontend/theme/theme/template/catalog/product/list.phtml
There is the following code that displays every product inside the category:
How can I extend this code and add a specific div after every 3 items, so I get something like this:
<div class="item-1"></div>
<div class="item-2"></div>
<div class="item-3"></div>
<div class="new-to-added-div"></div>
<div class="item-4"></div>
<div class="item-5"></div>
<div class="item-6"></div>
<div class="new-to-added-div"></div>
Solution
You should use the Modulo operator %
:
<?php $i = 0; foreach ($_productCollection as $_product): ?>
<?php if($i > 0 && $i%3 == 0): ?>
<div class="new-to-added-div"></div>
<?php else: ?>
<div class="item-<?php echo $i+1 ?>"></div>
<?php endif; ?>
<?php $i++ ?>
<?php endforeach; ?>
OTHER TIPS
<?php $i = 0; foreach ($_productCollection as $_product): ?>
<?php if ($i % 3 == 0): ?>
<div class="new-to-added-div"></div>
<?php else: ?>
<div class="item-<?php echo $i; ?>"></div>
<?php endif;?>
<?php $i++; ?>
<?php endforeach; ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange