Jump to content

Link using page ID

Peter Knight

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By Peter Knight
      I have a list of pages all based on the same template but I want to exclude 2 by page ID
      $expertises = $pages->find("template=case-type, sort=sort"); foreach ($expertises as $expertise); I know I could change their template or add an exclude checkbox to the template etc
      Wondering if there is a page=not(4989,9877) type of thing in the API to exclude pages by ID?
      I can't find anything in the API docs but think I may be searching for the wrong phrase.
    • By milo695
      Hi all,
      I need help creating link in CK editor that would target my translated page, so it depends on what language user has selected.
      something like:
      <a href="?id=1450">view all</a> would target:
      <a href="en/onion/">Onion</a> <a href="de/zwiebel"></a> depending on user defined language.
      Thank you
    • By SwimToWin
      I love nice urls - but I also recognize that they are not always needed nor desired due to the additional steps in page creation.
      Suggested solution:
      Page ID Page ID is a per-template auto-incrementing number. No. series might be added on a per-template basis (to be configured by webmaster). Page url Replace the full page url with an ID (as an optional configuration). Page url might then change to something like: www.example.com/page/1234 Editing (Page Tree is much less important in this scenario.) Like Drupal's Node IDs (nid).
      Why this change? Name may reveal confidential info. Name may not matter on certain templates. Page creation is no longer a two-step process; much like adding a row in MySQL.
      Benefit: This change will make it easier to use ProcessWire for other purposes than websites ("Oh - so ProcessWire is also a finance system!").
    • By gebeer
      Hi all,
      I'm working on a function that outputs markup for a bootstrap 3 carousel.
      I'm passing the $page->images object to the function argument $images.
      To have an individual id for each carousel on a page with multiple carousels, I want to use the image field id and the page id of the page were the image field sits.
      I can get the image field id with:
      $images->field->id But I can't seem to get the page id although it's there in the var_dump of the $images object:
      object(Pageimages)[203] protected 'page' => object(Page)[206] protected 'template' => ... protected 'settings' => array (size=10) 'id' => int 1013 //this is the id I need ... protected 'field' => object(Field)[72] protected 'settings' => array (size=5) 'id' => int 100 //this one I can get with $images->field->id ... When I try
      $images->page->settings->id I get null.
      When I try
      $images->page->id I get 1012 instead of 1013. 1012 is nowhere to be found in the $images object. It is the id of the page from which I'm calling my function. But I'm not passing the $page object to my function.
      So where does it come from and how can I get the correct page id?
      Thanks for reading through this!
    • By danielholanda
      I have this form in my html which works perfectly locally. Once I had this as a template my form doesnt work well. This has a calendar button available and here is the problem. I already had this php applied:
      <?php $myCalendar = new tc_calendar("date3", true, false); $myCalendar->setIcon("_php/calendar/images/iconCalendar.png"); $myCalendar->setPath("_php/calendar/"); $myCalendar->setYearInterval(2012, 2022); $myCalendar->dateAllow('2012-08-30', '2022-08-31'); $myCalendar->setDateFormat('j F Y'); $myCalendar->writeScript(); ?> This means that I would have to paste this code which I have applied to any inner url I have in the html such as this example:
      <input type="image" src="<?php echo $config->urls->templates?>_img/submit_button.png" id="input_img" name="submit" /> I've tried inserting only echo $config->urls->templates?. This way:
      <?php $myCalendar = new tc_calendar("date3", true, false); $myCalendar->setIcon("$config->urls->templates_php/calendar/images/iconCalendar.png"); $myCalendar->setPath("$config->urls->templates_php/calendar/"); $myCalendar->setYearInterval(2012, 2022); $myCalendar->dateAllow('2012-08-30', '2022-08-31'); $myCalendar->setDateFormat('j F Y'); $myCalendar->writeScript(); ?> If I wanted the url to work how should I do it with processwire?
  • Create New...