0

I am using the below on my 2columns-left.phtml template to detect whether the page is a category page or a product page.

  <?php $_catDetails = Mage::registry('current_category') ?>

  <?php echo Mage::registry('current_category')->getName();?>
  <?php echo Mage::registry('current_category')->getParentId();?>

        <?php if(Mage::registry('current_category')){ ?>
            <?php echo 'I am a category page'; ?>
        <?php } else { ?>
            <?php echo 'I am not a category page'; ?>
        <?php } ; ?>

However this appears to be also echoing on a product page!

Can anyone explain what i am doing wrong, as i thought the first line would detect the Mage page type registration.

Thanks Kirsty

Update I have also tried the below:

<?php if($this->getCategoryPage()) {
            echo 'This is a category page';
        }else {
            echo 'this is not a category page';
        } ?>
  • Do you mind giving me some context on what exactly you are trying to achieve? – André Ferraz Feb 21 '18 at 12:17
1

You can check like this:

<?php 
$action = Mage::app()->getFrontController()->getAction()->getFullActionName();
if ($action == 'catalog_category_view') {
   //you are in the category page
} elseif ($action == 'catalog_product_view') {
   //you are in the product page
} else {
   //you are somewhere else.
}
  • Hi Marius, thanks for this. It appears however, that this only worked on the category view and not the product view?? What is the difference between the catalog cat view and the product cat view? Thankyou :) – Kirsty Marks Feb 21 '18 at 12:20
  • Sorry. I made a mistake. In the elseif part I had product_category_view instead of catalog_product_view. Now it should be OK. – Marius Feb 21 '18 at 12:22
  • Hi Marius. Wonderful. This seems a nicer way to write the requirements. Great help. – Kirsty Marks Feb 21 '18 at 12:26
  • Is there any benefits in using the above method over the one below?? – Kirsty Marks Feb 21 '18 at 12:26
  • @KirstyMarks use this because Marius said so.. – Qaisar Satti Feb 21 '18 at 12:36
0

When you go from category page to product then category registry also created so add additional check of Mage::registry('current_product') will do the trick here

<?php $_catDetails = Mage::registry('current_category') ?>

  <?php echo Mage::registry('current_category')->getName();?>
        <?php echo Mage::registry('current_category')->getParentId();?>

        <?php if(Mage::registry('current_category') && !Mage::registry('current_product')){ ?>
            <?php echo 'I am a category page'; ?>
        <?php } else { ?>
            <?php echo 'I am not a category page'; ?>
        <?php } ; ?>
  • I will accept your answer in 5 mins when i can :) – Kirsty Marks Feb 21 '18 at 12:13

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.