Magento All products with images first (list.phtml)

Magento All products with images first (list.phtml)

October 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento All products with images first (list.phtml)

php echo $this->getPriceHtml($_product, true) ?> <div class=“actions”> <?php if($_product->isSaleable()): ?> <button type=“button” title=”<?php echo $this->(‘Add to Cart’) ?>” class=“button btn-cart” onclick=“setLocation(’<?php echo $this->getAddToCartUrl($_product) ?>’)”><span><span><?php echo $this->(‘Add to Cart’) ?></span></span></button> <?php else: ?> <p class=“availability out-of-stock”><span><?php echo $this->(‘Out of stock’) ?></span></p> <?php endif; ?> <ul class=“add-to-links”> <?php if ($this->helper(‘wishlist’)->isAllow()) : ?> <li><a href=”<?php echo $this->helper(‘wishlist’)->getAddUrl($_product) ?>” class=“link-wishlist”><?php echo $this->(‘Add to Wishlist’) ?></a></li> <?php endif; ?> <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?> <li><span class=“separator”>|</span> <a href=”<?php echo $_compareUrl ?>” class=“link-compare”><?php echo $this->__(‘Add to Compare’) ?></a></li> <?php endif; ?> </ul> </div> </li> <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?> </ul> <?php endif ?> <?php else: ?> <?php $imgprd[]=$_product ?> <?php endif; ?> <?php endforeach ?> <?php foreach ($imgprd as $_product): ?>

&lt;?php if ($i++%$_columnCount==0): ?&gt;
&lt;ul class="products-grid"&gt;
&lt;?php endif ?&gt;
    &lt;li class="item&lt;?php if(($i-1)%$_columnCount==0): ?&gt; first&lt;?php elseif($i%$_columnCount==0): ?&gt; last&lt;?php endif; ?&gt;"&gt;
        &lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $this-&gt;stripTags($this-&gt;getImageLabel($_product, 'small_image'), null, true) ?&gt;" class="product-image"&gt;&lt;img src="&lt;?php echo $this-&gt;helper('catalog/image')-&gt;init($_product, 'small_image')-&gt;resize(135); ?&gt;" width="135" height="135" alt="&lt;?php echo $this-&gt;stripTags($this-&gt;getImageLabel($_product, 'small_image'), null, true) ?&gt;" /&gt;&lt;/a&gt;
        &lt;h2 class="product-name"&gt;&lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $this-&gt;stripTags($_product-&gt;getName(), null, true) ?&gt;"&gt;&lt;?php echo $_helper-&gt;productAttribute($_product, $_product-&gt;getName(), 'name') ?&gt;&lt;/a&gt;&lt;/h2&gt;
        &lt;?php if($_product-&gt;getRatingSummary()): ?&gt;
        &lt;?php echo $this-&gt;getReviewsSummaryHtml($_product, 'short') ?&gt;
        &lt;?php endif; ?&gt;
        &lt;?php echo $this-&gt;getPriceHtml($_product, true) ?&gt;
        &lt;div class="actions"&gt;
            &lt;?php if($_product-&gt;isSaleable()): ?&gt;
                &lt;button type="button" title="&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;" class="button btn-cart" onclick="setLocation('&lt;?php echo $this-&gt;getAddToCartUrl($_product) ?&gt;')"&gt;&lt;span&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;&lt;/span&gt;&lt;/span&gt;&lt;/button&gt;
            &lt;?php else: ?&gt;
                &lt;p class="availability out-of-stock"&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Out of stock') ?&gt;&lt;/span&gt;&lt;/p&gt;
            &lt;?php endif; ?&gt;
            &lt;ul class="add-to-links"&gt;
                &lt;?php if ($this-&gt;helper('wishlist')-&gt;isAllow()) : ?&gt;
                    &lt;li&gt;&lt;a href="&lt;?php echo $this-&gt;helper('wishlist')-&gt;getAddUrl($_product) ?&gt;" class="link-wishlist"&gt;&lt;?php echo $this-&gt;__('Add to Wishlist') ?&gt;&lt;/a&gt;&lt;/li&gt;
                &lt;?php endif; ?&gt;
                &lt;?php if($_compareUrl=$this-&gt;getAddToCompareUrl($_product)): ?&gt;
                    &lt;li&gt;&lt;span class="separator"&gt;|&lt;/span&gt; &lt;a href="&lt;?php echo $_compareUrl ?&gt;" class="link-compare"&gt;&lt;?php echo $this-&gt;__('Add to Compare') ?&gt;&lt;/a&gt;&lt;/li&gt;
                &lt;?php endif; ?&gt;
            &lt;/ul&gt;
        &lt;/div&gt;
    &lt;/li&gt;
&lt;?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?&gt;
&lt;/ul&gt;
&lt;?php endif ?&gt;
&lt;?php endforeach ?&gt;
&lt;script type="text/javascript"&gt;decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])&lt;/script&gt;

<?php endif; ?>

<div class=“toolbar-bottom”> <?php echo $this->getToolbarHtml() ?> </div>

The code provided appears to be a section of a Magento template file, specifically dealing with the rendering of product items in a grid layout. Let me break down what’s happening:

  1. The initial foreach loop iterates through each product item in the collection.
  2. Inside this loop, it checks if the current iteration is divisible by $_columnCount or if it’s the last element. If either condition is true, it closes the current <ul> tag and starts a new one.
  3. For each product item, it generates an HTML list item (<li>) with several elements:
    • A hyperlink to the product page, displaying the product name using the getProductUrl and productAttribute methods.
    • An image of the product using the helper('catalog/image') method to resize the small image.
    • The product’s rating summary if available.
    • The price of the product rendered by getPriceHtml($_product, true).
    • A “actions” div containing:
      • An “Add to Cart” button for saleable products or an “Out of stock” message for non-saleable ones.
      • A wishlist link if allowed by Magento.
      • A comparison link if available.
  4. After the main foreach loop, it checks if there’s any product item left in $imgprd. If so, it processes them using a second foreach loop (which seems redundant).
  5. The script then decorates the generated grid with CSS classes and adds a toolbar at the bottom rendered by getToolbarHtml().

Overall, this code snippet is responsible for generating HTML to display products in a grid layout on a Magento site, showcasing product images, names, prices, and interactive elements like “Add to Cart”, “Wishlist”, and “Comparison”.

Checkout Catalog Themes Pricing

Our network