how to link to category page from phtml?
-
11-12-2019 - |
Question
I have a category page called "team", which is nested in my menu structure.
How can I link to that page and url, from within a phtml page elsewhere? I am of course wanting it to be dynamic.
For images I've been using:
echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).
but I am currently braindead on how to do that with category pages.
(side note - I use multi-language, multi-views.)
Solution
You can create a link with current web URL by using
Mage_Core_Model_Store::URL_TYPE_WEB
i.e.
<a href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).'company/team.html'?>">meet the complete team</a>
Then to make it internationalized ... 1 approach that comes to mind (and after consulting with google for something better) ... is to add translation.
If you are using a your own module for the above changes, you can:
- for example for a Italian storeview, create an YourNamespace_YourModule.csv at app/locale/it_IT/
in that /it_IT/YourNamespace_YourModule.csv add the following lines
"company/team","società/squadra"
where ""società/squadra" are the names of the category/sub in Italian and
"meet the complete team","the link text in Italiano goes here"
after that modify the link to be
<?php $my_helper = Mage::getHelper('yournamespace_yourmodule')?> <a href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).$my_helper->__('company/team')'.html'?>"><?php echo $my_helper->__('meet the complete team')</a>
where in Mage::getHelper('yournamespace_yourmodule') 'yournamespace_yourmodule' should be the XML path to your Helper, as defined in your config.xml
repeat for any language/storeview you might need
If you are not using your own helper, you can add the above translations to a core module translation file and call the respective core module Helper in the link.
Hopefully, someone of the Magento gurus could provide a more canonical approach to the task, if not, the outlined approach seems reasonable enough to me.