23

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

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Gowri
  • 16,587
  • 26
  • 100
  • 160

7 Answers7

41

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.

Ashwani Panwar
  • 3,819
  • 3
  • 46
  • 66
Josh
  • 2,820
  • 23
  • 15
  • Is there a way to make this work too in the wysiwyg editor, to be able to link to a certain productpage? In one product I want to link to another product in the discription field, without copy/pasting the (current) SEO url. Better would be to make a link like other product. Is that possible? Because PHP is ofcourse not a option from within the WYSIWYG editor ... – Dr.Bob May 24 '13 at 11:02
  • 3
    @ButtleButkus: Thanks for intimating – Gowri Jul 06 '13 at 20:31
  • @Josh What if i want url by store / language? – Vipul Hadiya May 22 '15 at 07:39
  • Just what i was looking for! Awesome! +1 – mjcoder Apr 01 '16 at 09:15
19

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

Anton S
  • 12,750
  • 2
  • 35
  • 37
  • +1 it's working but if i change the URL key whats_new to whats_new1 it will not work am i right . is there any good thing available in magento – Gowri Sep 15 '11 at 12:23
  • I edited my answer, there's no magic and you still have to set your variables – Anton S Sep 15 '11 at 12:39
  • 1
    If the value passed happens to be the name of a module then it gets rewritten to that module's front name. For example "adminhtml" becomes "admin". To avoid such potential rewrites try doing it like `getUrl('', array('_direct'=>$yourDynamicVariable))`. See [this wiki](http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/geturl_function_parameters) for more details. – clockworkgeek Sep 15 '11 at 17:45
  • 3
    Which class is `$this` in this answer? – feeela Aug 08 '12 at 11:34
  • print_r(get_class($this)); but i guess some class inherited from template class – Anton S Aug 08 '12 at 12:12
  • @AntonS S so this code probably won't work in controllers and models… – feeela Dec 03 '12 at 12:27
  • @feeela feel free to replace $this with any object you can ask this method from to use in models and controllers – Anton S Dec 03 '12 at 13:03
9

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

joseantgv
  • 1,943
  • 1
  • 26
  • 34
5

In CMS Page

{{store _direct="url_key"}}

If you want in .phtml file then

<?php echo Mage::helper('cms/page')->getPageUrl('url_key') ?>
Bikram Pahi
  • 1,107
  • 1
  • 12
  • 33
2
Mage::getUrl(null, array('_direct' => $page->getIdentifier()));
Tuong Le
  • 18,533
  • 11
  • 50
  • 44
  • That is the correct way.. it works also with an arbitrary identified, i.e. `Mage::getUrl(null, array('_direct' => $url_key));` – zontar May 03 '15 at 12:55
1

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

You shoud use

{{store direct_url="whats_new/"}}

<?php echo $this->getUrl('whats_new');?>
tungpksa
  • 21
  • 1