0

I have the following code in product/view.phtml:

<?php 
    if(Mage::registry('current_category')):
    $_category=Mage::registry('current_category');
    $_helper    = Mage::helper('catalog/output');
        $_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) { ?>
    <!-- Category has Image uploaded -->
    <?php echo $_imgHtml ?>
    <?php }else{ ?>
    <!-- Category has NO Image uploaded, show static block -->
    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cms_top_banner')->toHtml(); ?>
<?php }?>
<?php endif;?>

This currently shows either the category banner or a static banner if the category does not currently have a banner. However, if I visit the product page directly then no banner is displayed.

Can anyone please suggest an edit to the code so that if a user visits a product directly (i.e. not by visiting a category) then the static banner ('cms_top_banner') will be displayed.

  • That is because Mage::registry('current_category') contain value when you browse products from category page – Minesh Patel Oct 9 '15 at 6:16
2

The problem here is, when you are loading your product page directly, Mage::registry('current_category') will return NULL and thus nothing will get executed in your code, since all of your code resides in that parent if loop checking.

You can use below code.

<?php
    $_imgHtml = '';
    $_imgUrl  = false;

    //check whether a valid category exist
    if(Mage::registry('current_category')) {

        //grabing category banner if any
        $_category=Mage::registry('current_category');
        $_helper    = Mage::helper('catalog/output');
        if ($_imgUrl = $_category->getImageUrl()) {

            //prepare banner html
            $_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');
        }
    }

    //check a valid image url exist
    if ($_imgUrl) {
            echo $_imgHtml;
    } else {
        echo $this->getLayout()->createBlock('cms/block')
            ->setBlockId('cms_top_banner')
            ->toHtml();
    }
?>

This will make sure, if a valid category does not exist, it will show your default banner in any case.

1

You can try this code, it will show static banner if there is no category:

<?php
    if(Mage::registry('current_category')):
        $_category=Mage::registry('current_category');
        $_helper    = Mage::helper('catalog/output');
        $_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) { ?>
            <!-- Category has Image uploaded -->
            <?php echo $_imgHtml ?>
        <?php }else{ ?>
            <!-- Category has NO Image uploaded, show static block -->
            <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cms_top_banner')->toHtml(); ?>
        <?php }?>
<?php else: ?>
        <!-- No category available, show static block -->
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cms_top_banner')->toHtml(); ?>
<?php endif; ?>
  • Hi Prateek, I receive an unexpected 'endif' error with the code above. – YorkieMagento Oct 9 '15 at 6:29
  • You might be having that at the end of your code somewhere. Try removing the last endif in above code. – Prateek Oct 9 '15 at 6:31
1

Try this

 <?php if(Mage::registry('current_category')):
        $_category=Mage::registry('current_category');
        $_helper    = Mage::helper('catalog/output');
        $_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');
        endif;?>
      <?php if($_imgUrl) : ?>
        <?php echo $_imgHtml ?>
      <?php else: ?>
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cms_top_banner')->toHtml(); ?>
      <?php endif;?>
<?php else:?>
    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cms_top_banner')->toHtml(); ?> 
<?php endif;?>
  • Hi Minesh, I receive an unexpected 'endif' error with the code above. – YorkieMagento Oct 9 '15 at 6:29
  • @YorkieMagento just updated code please check – Minesh Patel Oct 9 '15 at 6:33

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.