Magento – Check page url is category page or cms

categorycmsmagento-1.9pageurl

I have an page url of magento site.

my question is : how can i check the page url is category page or cms page or other page?

for example:

page URL is : http://example.com/craft.html(category page) or http://example.com/terms-policy(cms page)

i want to check page url is category page or cms page.

Thanks.

Best Answer

I think the answer given by Minesh in comments are partially correct. If you are trying to check it in your template file you can use below codes to check whether you are in category page, cms page or product page.

To check if you are in category page:

if (Mage::registry('current_category'))
{
  // category page
}

To check if it is a cms page:

if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms')
{
  // CMS page
}

To check if it is a product page:

if(Mage::registry('current_product'))
{
  // Product detail page
}

Edited

From your comment, below code will let you know the last page url is either cms page, or category or product.

$lastUrl = Mage::getSingleton('core/session')->getLastUrl();
    if (preg_match("#cms/index/index#", $lastUrl)) {
        return "Last page was:<br>Home page<br/>url: ".$lastUrl;
    }
    elseif(preg_match("#catalog/category/view#", $lastUrl)) {
        return "Last page was:<br>Category page<br/>url: ".$lastUrl;
    }
    elseif (preg_match("#catalog/product/view#", $lastUrl)) {
        return "Last page was:<br>Product detail page<br/>url: ".$lastUrl;
    }
    else
    {
        return "Last page was:<br>Unknown page<br/>url: ".$lastUrl;
    }

I hope this will help. If it works for you pleae don't forget to accept and vote the answer. This will help others too.

Happy Coding!!

Related Topic