Magento – Product images does not show in collection

collection;image

I am learning Magento. I am trying to list out my product images.

Firstly I tried with a single product and it worked:

<?php
$productModel = Mage::getModel('catalog/product');
$product_1 = $productModel->load(1);
?>
<div><a href="<?php echo $product_1->getProductUrl();?>"><img src="<?php echo $product_1->getImageUrl()?>" /></a></div>

Then, I create a collection to show a list of pictures, but it showed all Magento logos, not product pictures. (I checked base, small and thumbnail image in admin already)

<?php
  $product_collection = Mage::getModel('catalog/product')->getCollection()->setOrder('name', 'asc'); 
?>
  <div class="tai-slider2">
<?php
  foreach($product_collection as $_product)
       {
?>
  <div><a href="<?php echo $_product->getProductUrl();?>"><img src="<?php echo $_product->getImageUrl()?>" /></a></div>
<?php
        }  
?>

Please help! Thank you!

Best Answer

Maybe try adding image attribute to your collection will do the trick

$product_collection = Mage::getModel('catalog/product')
                            ->getCollection()
                            ->addAttributeToSelect('image')
                            ->setOrder('name', 'asc'); 

<div class="tai-slider2">
    <?php foreach ($product_collection as $_product) { ?>
        <div>
            <a href="<?php echo $_product->getProductUrl();?>">
                <img src="<?php echo $_product->getImageUrl()?>" />
            </a>
        </div>
    <?php } ?>
</div>
Related Topic