23
votes

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')?

7

7 Answers

41
votes

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.

19
votes

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

9
votes

You shoud use <?php echo Mage::getUrl('page-url.html); ?>

5
votes

In CMS Page

{{store _direct="url_key"}}

If you want in .phtml file then

<?php echo Mage::helper('cms/page')->getPageUrl('url_key') ?>
2
votes
Mage::getUrl(null, array('_direct' => $page->getIdentifier()));
1
votes

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') ?>
0
votes

You shoud use

{{store direct_url="whats_new/"}}

<?php echo $this->getUrl('whats_new');?>