Magento: How to tell if you're on a category page, or product page in .phtml file(s)

前端 未结 4 733
余生分开走
余生分开走 2021-02-02 11:57

I am trying to program into my .phtml files an if statement if the guest is on a category list page, or on a product page.

For example this code:



        
4条回答
  •  南方客
    南方客 (楼主)
    2021-02-02 12:13

    While Alan's answer will work, there is a more direct option, and you were actually on the right track with your code snippet... you just need to inspect the controller name rather than the module name:

    getFrontController()->getRequest()->getControllerName(); ?>
    

    That will return category or product based on their controllers being CategoryController.php and ProductController.php respectively.

    This does assume that you've not installed any third-party modules that rewrite those controllers with their own.

提交回复
热议问题