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.

General Questions /

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

Get link by ID for nested pages


Reply


4 Posts   2637 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, 1132 Posts

17 January 2011 at 1:09am

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

should work