I have a block on my front page that is grabbing the two newest products with a custom product image called image_feature_front_right.
I use this code as the query:
$_helper = $this->helper('catalog/output');
$_productCollection = Mage::getModel("catalog/product")->getCollection();
$_productCollection->addAttributeToSelect('*');
$_productCollection->addAttributeToFilter("image_feature_front_right", array("notnull" => 1));
$_productCollection->addAttributeToFilter("image_feature_front_right", array("neq" => 'no_selection'));
$_productCollection->addAttributeToSort('updated_at', 'DESC');
$_productCollection->setPageSize(2);
I am able to get:
- the image:
echo $this->helper('catalog/image')->init($_product, 'image_feature_front_right')->directResize(230,315,4) - the product url:
echo $_product->getProductUrl() - the product name:
$this->stripTags($_product->getName(), null, true)
The only thing I am unable to get is the custom images label. I have tried echo $this->getImageLabel($_product, 'image_feature_front_right'), but that seems to do nothing.
Can anyone point me in the right direction?
Thanks!
Tre
I imagine this is some sort of magento bug. The issue seems to be that the Magento core is not setting the custom_image_label attribute. Whereas for the default built-in images [image, small_image, thumbnail_image] it does set these attributes – so you could do something like:
If you look at
Mage_Catalog_Block_Product_Abstract::getImageLabel()it just appends ‘_label’ to the$mediaAttributeCodethat you pass in as the 2nd param and calls$_product->getData().If you call
$_product->getData('media_gallery');you’ll see the custom image label is available. It’s just nested in an array. So use this function:It’d be prudent to extend the Magento core code (Ideally
Mage_Catalog_Block_Product_Abstract, but I don’t think Magento lets you override Abstract classes), but if you need a quick hack – just stick this function in your phtml file then call: