0

I have the following code in a custom block that is referenced in my three column layout. To get the category banner to show at the very top of each category page.

<?php
$_helper    = $this->helper('catalog/output');
$_category  = $this->getCurrentCategory();
$_imgHtml   = '';
if ($_imgUrl = $_category->getImageUrl()) {
    $_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->escapeHtml($_category->getName()).'" title="'.$this->escapeHtml($_category->getName()).'" /></p>';
    $_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
?>


<?php if($_imgUrl): ?>
<?php echo $_imgHtml ?>
<?php endif; ?>

However I get a fatal call to object error, can anyone help? I've tried a simple echo hello to test there's nothing wrong with the block. So there's some issue with the code above. Thank you.

Here's the error..

fatal error: Call to a member function getImageUrl() on a non-object in /home/website/public_html/app/design/frontend/mytheme/default/template/custom/ca‌​tegory/top_banner.phtml on line 5

2

This is because you are not using the right block class in this case. I am sure about this, because I know you are using Mage_Core_Block_Template block if you have followed my answer in your previous thread.

Let us look what is gone wrong here. The error says you are calling getImageUrl() on a non-object. This means $_category is a non-object. Now as per your template code, $_category is defined like this.

$_category  = $this->getCurrentCategory();

This means you are expecting $_category should hold the current category. But what really happened is it holds a NULL value since $this->getCurrentCategory() returns nothing. A quick solution for this is,

$_category = Mage::registry('current_category');
if ($_category) {
    //then show your banner code.
} else {
    //do nothing
}

Most of the case, it will work. If you template is more complex than this, then you may need to create a custom block for showing banner and then use that block instead of Mage_Core_Block_Template block.

Let me know your feedback after try this.

EDIT

I tested below code and it works well for me.

<?php
$_helper    = $this->helper('catalog/output');
$_category  = Mage::registry('current_category');
$_imgHtml   = '';
if ($_imgUrl = $_category->getImageUrl()) {
    $_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->escapeHtml($_category->getName()).'" title="'.$this->escapeHtml($_category->getName()).'" /></p>';
    $_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
?>

<?php if($_imgUrl): ?>
<?php echo $_imgHtml ?>
<?php endif; ?>
  • Works Rajeev, thanks very much. Could this block also now be referenced to show the banner on the product view page? – YorkieMagento Oct 1 '15 at 7:25
  • @user2046646 it only works in category page. – Rajeev K Tomy Oct 1 '15 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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