Jump to content

Link using page ID


Peter Knight
 Share

Recommended Posts

I'd like to link to a page by referencing it's page ID 

<a href="?id=1411">view all</a>

instead of using a page path

<a href="/solutions/case-studies/">view all</a>

Using a page name/path  result in breaking changes whereas using an ID is more future proof, right?

The only Module I found is called PageLinkAbstractor which works for text fields and RTEs but not necessarily templates. 

I normally do this on MODX using something like...

<a href="index.php?id=1411">view all</a>
Link to comment
Share on other sites

For relative URL, you can use:

<a href="<?php echo $pages->get(23)->url;?>">Page with ID 23</a>
If you want the full http URL, use ->httpUrl instead of ->url.

Edited to add: Sorry, forgot to add the 's' to $pages - totally different variable :)

  • Like 2
Link to comment
Share on other sites

Using a page name/path result in breaking changes whereas using an ID is more future proof, right?

I have never liked the principle of using ID's that way. And I will never use it this way unless i'm absolute sure that the page can't be deleted or the ID is fixed in the core (homepage / trash / admin etc.). The problem with ID's is that when you delete the page, your ID is gone and your script will break. And a minor thing with ID's is that the ID wil not tell you anything about the page, so you need to comment it.

I think it's almost always better to get a page from it context. Context of a page can be the template, the position of the page relative to an other, the amount of parents or kind of children. Or maybe even a combination of those factors.

When you program with 'context' your client wil never be able to break your site.

  • Like 1
Link to comment
Share on other sites

When you program with 'context' your client wil never be able to break your site.

If the client deletes a page it's gone, no matter how you call it :). It's still better to not use id's as they are one timers, while a page that's called by context can be recreated and used further without changing the code.

Link to comment
Share on other sites

The only Module I found is called PageLinkAbstractor which works for text fields and RTEs but not necessarily templates. 

PageLinkAbstractor has been deprecated (Ryan has removed it from the modules directory). The core PagePathHistory is his preferred approach to dealing with changed URLs now.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...