Jump to:

22972 Posts in 11596 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » Get link by ID for nested pages

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2303 Views
  • DeklinKelly
    Avatar
    Community Member
    196 Posts

    Get link by ID for nested pages Link to this post

    How can I get the link of a page by its ID?

    This code only works if nested URLs are disabled. It does NOT work for nested URLs:

    $Link = Director::BaseURL() . DataObject::get_by_id("SiteTree", $page->ID)->URLSegment;

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: Get link by ID for nested pages Link to this post

    Use the Link method rather than trying to build it with the URLSegment,

    try:

    $Link = DataObject::get_by_id("SiteTree", $page->ID)->Link;

  • DeklinKelly
    Avatar
    Community Member
    196 Posts

    Re: Get link by ID for nested pages Link to this post

    Howard, that does not work. It does not provide a full actual link either. The URLSegment field in the SiteTree database does not contain the full url, only the URLSegment of the final child page. How can I get the FULL URL Segment as impacted by parent pages?

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: Get link by ID for nested pages Link to this post

    $Link = DataObject::get_by_id("SiteTree", $page->ID)->Link();

    should work

    2303 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.