Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Get link by ID for nested pages


Go to End


4 Posts   6013 Views

Avatar
DeklinKelly

Community Member, 197 Posts

16 January 2011 at 1:16pm

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;

Avatar
Howard

Community Member, 215 Posts

16 January 2011 at 3:29pm

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

try:

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

Avatar
DeklinKelly

Community Member, 197 Posts

16 January 2011 at 3:42pm

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?

Avatar
martimiz

Forum Moderator, 1391 Posts

17 January 2011 at 1:09am


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

should work