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.

Template Questions /

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

Control a page by ID rather than URL?


Go to End


5 Posts   6572 Views

Avatar
neilcreagh

Community Member, 136 Posts

19 May 2010 at 7:31am

In my templates, how would I add specific page control by ID rather than URL? eg.

<% control Page(PageID) %>

<% control ChildrenOf(PageID) %>

On a couple of occasions a client will inadvertently break these links by changing the page title (and/or the meta title) - so controlling by URL is not always ideal.

Any ideas?

Avatar
bummzack

Community Member, 904 Posts

19 May 2010 at 6:26pm

You could write a function that does that for you quite easily.
Place this in your Page class:

function PageById($id){
	return DataObject::get_by_id("SiteTree", (int)$id);
}

Then in your template you can access pages by id like this:

<% control PageById(1) %>$Title<% end_control %>

Avatar
web2works

Community Member, 50 Posts

12 June 2010 at 10:28am

thank you this has helped me out a lot.

Avatar
NickJacobs

Community Member, 148 Posts

30 June 2010 at 8:57am

You can also just simply use the ID in place of the URLSegment,ie:

<% control childrenOf(20) %>

Avatar
Newtothis

Community Member, 4 Posts

25 July 2014 at 2:33pm

Edited: 25/07/2014 2:34pm

Hi There,
This looks like the sort of code I need too. I have ids that are returning dynamically created page numbers but i really need the page name or something (someone helped me with this and its in the Navigation.ss file). My problem is I don't know where to add the code suggested on the forum. (I'm a designer muddling through!) Does it go under Template>layouts Page.ss or in the includes .ss files somewhere or in the code> page.php file? I really have no idea!
Would appreciate the clarification,
Cheers!
Claire