0

I'm trying to add an image link to the head of my pages. This code below works except if the image isn't there, I've tried a lot of different ways but no luck. The part <?php echo $this->getImage(); ?> works fine on it's on but it doesn't get the correct image, so I'm using it to fall back on if the other image isn't there. But I keep getting errors and the page goes blank if the image isn't there. Here's my latest attempt.

<?php if (($this->helper('catalog/image')->init(Mage::registry('current_product'), 'twitter_image')) != null): ?>
    <?php $twitter_image = $this->helper('catalog/image')->init(Mage::registry('current_product'), 'twitter_image'); ?> 
    <?php if ($twitter_image): ?><?php echo $twitter_image; ?>
    <?php else: ?>
    <?php echo $this->getImage(); ?>
    <?php endif; ?>
    <?php endif; ?>

Any suggestions?

Thanks

1
  • are u trying on product page ? Commented Jul 25, 2016 at 13:04

3 Answers 3

1

try getimagesize

    <?php if (getimagesize($twitter_image)!== false): ?><?php echo $twitter_image; ?>
    <?php else: ?>
    <?php echo $this->getImage(); ?>
    <?php endif; ?>

another solution

<?php $products=Mage::getModel('catalog/product')->load(Mage::registry('current_product')->getId());
if ($products->getTwitterImage()): ?>
<?php $twitter_image = $this->helper('catalog/image')->init(Mage::registry('current_product'), 'twitter_image'); ?> 
<?php echo $twitter_image; ?>
<?php else: ?>
<?php echo $this->getImage(); ?>

<?php endif; ?>
7
  • No, that doesn't work at all, errors everywhere, even if the image is there. : )
    – Freejoy
    Commented Jul 25, 2016 at 12:55
  • @Freejoy added another solution.. Commented Jul 25, 2016 at 13:09
  • No, still doesn't work. If I use this it works on the pages with the image and blanks out if the image isn't there: <?php $twitter_image = $this->helper('catalog/image')->init(Mage::registry('current_product'), 'twitter_image'); ?> <?php if ($twitter_image): ?><?php echo $twitter_image; ?> <?php else: ?> <?php echo $this->getImage(); ?> <?php endif; ?> It gets the correct image too.
    – Freejoy
    Commented Jul 25, 2016 at 13:19
  • added the way from registry and also check your attribute is loading or not twitter_image Commented Jul 25, 2016 at 13:22
  • you have to check for value before getting the image otherwise you will get the error. Commented Jul 25, 2016 at 13:24
0

You need to check your error log / php logs to see what's killed the page.

What looks weird to me immediately is this line:

<?php if (($this->helper('catalog/image')->init(Mage::registry('current_product'), 'twitter_image')) != null): ?>

This is the function you're calling:

/** 135 * Initialize Helper to work with Image 136 * 137 * @param Mage_Catalog_Model_Product $product 138 * @param string $attributeName 139 * @param mixed $imageFile 140 * @return Mage_Catalog_Helper_Image 141 */ 142 public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)

It doesn't ever return null, it returns an instance of Mage_Catalog_Helper_Image. If you want to check if the product has the attribute you're better off doing something like

$_product = Mage::registry('current_product); if ($product && $product->getTwitterImage()) { }

1
  • No errors but it didn't get a link.
    – Freejoy
    Commented Jul 25, 2016 at 13:28
0
 Please try below code.

<?php
$product_id = Mage::registry('current_product')->getId();
$_product = Mage::getModel('catalog/product')->load($_productId);
$twitter_image = $this->helper('catalog/image')->init($_product, 'twitter_image')->resize(100);
    if($twitter_image != '' && $twitter_image != 'no_selection'){
    ?>
    <img src='<php echo$twitter_image ; ?>' />
    <?php
    }
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.