Reputation: 16835
I would like to get a page's URL key in Magento.
For example, I have a CMS page called "What's New" with the identifier (or URL key) "whats_new". Its correct URL is therefore http://mysite.com/whats_new
Currently I use this code to echo its location:
<?php echo Mage::getBaseUrl();?>whats_new
I feel it's bad practice because its identifier (or URL key) is administrable; if its URL key or identifier changes then the link will break. What is the proper way to echo its dynamic URL key? Perhaps something similar to Wordpress's get_permalink('10')
?
Upvotes: 23
Views: 57481
Reputation: 2820
I think this will do what you want:
<?php echo Mage::helper('cms/page')->getPageUrl( $pageId ) ?>
Replace $pageId
with the correct id for the page you are linking to and it should work.
Upvotes: 41
Reputation: 31
You shoud use
{{store direct_url="whats_new/"}}
<?php echo $this->getUrl('whats_new');?>
Upvotes: 0
Reputation: 1183
In CMS Page
{{store _direct="url_key"}}
If you want in .phtml file then
<?php echo Mage::helper('cms/page')->getPageUrl('url_key') ?>
Upvotes: 5
Reputation: 165
It is also possible to retrieve the CMS page URL using the page identifier like,
<?php echo Mage::helper('cms/page')->getPageUrl('cms_page_identifier') ?>
Upvotes: 1
Reputation: 12750
Try this
<?php echo $this->getUrl('whats_new');?>
If you need to add url key dynamically then
<?php echo $this->getUrl($yourDynamicVariable);?>
of course you must implement the features that you need to fill the variable if url key is changed
Upvotes: 19