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.

Template Questions /

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

How to create a link to an id

Go to End

9 Posts   8803 Views


Community Member, 49 Posts

28 April 2009 at 8:15pm

Edited: 28/04/2009 8:17pm

I'm trying to create a link in my template to link to a particular page in the site tree.

I've tried using LinkToID to link a page in the sitetree that has the id of 10 but have been unable to figure how this or if this is the correct method:

<a href="$LinkToID.10">Link</a>

How can i add a link in a template that links to a page in site tree? Is $LinkToID the correct method?


Community Member, 148 Posts

2 May 2009 at 2:51pm

Hi Hammy, this may not be the best or most elegant way to do it, but after hacking at it for a while, this worked for me :)
I have a FeaturedPage pagetype which then links back to a product category (or any other page) on the site:

class FeaturedPage extends Page {
   static $has_one = array(
   'FeatureImage' => 'Image',
   'ProductCatLink' => 'Page'
   function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab('Root.Content.Features', new TreeDropdownField("ProductCatLinkID", "Select a page to link to", "SiteTree")); 
	$fields->addFieldToTab("Root.Content.Features", new ImageField('FeatureImage',"Feature Image"));	
        return $fields;
class FeaturedPage_Controller extends Page_Controller {

then in the template I do:

<a class="redir" href="<% control ProductCatLink %> $URLSegment <% end_control %>">More info</a>

Hope that helps in some way


Community Member, 7 Posts

30 May 2009 at 3:17pm

Hi there,

I don't know if there's a better way of doing it, but I put a function in my Page.php..

    function GetLinkFromID($id = 1) {
        $do = DataObject::get_one('Page', '`SiteTree_Live`.ID = '.$id, true);
        return ($do->URLSegment);

This will return the link to any page (or class extending page).
In your template you'd have something along the lines of
<a href="$GetLinkFromID(12)">

where 12 is obviously the id of the page you're wanting to link to.

If you're within a <% control %> loop, you may need to use $Top.GetLinkFromID(12)

Hope that helps.

If you found a better way of doing it I'd be interested to hear. :)


Forum Moderator, 435 Posts

22 June 2009 at 10:03pm

Edited: 22/06/2009 10:05pm

@NickJacobs: But this link only to ProductCatLink page...


Community Member, 7 Posts

5 July 2009 at 12:01pm

It will link to whatever page relates to the id you use in the function..
e.g. using $GetLinkFromID(12) will return the link to whatever has an id of 12.
Using $GetLinkFromID(13) will return the link to whatever has an id of 13, etc..


Community Member, 135 Posts

13 November 2009 at 1:30am


I am trying to use this and all works fine until I try to put this in

<a href="$GetLinkFromID($StoryUrl)" >

... is there a problem dropping $StoryUrl in intsead of nmiuber. It works when i put a number in... and $StoryUrl works by outputting number when not in the stated string?????

Anyone know why??


Community Member, 904 Posts

13 November 2009 at 2:33am

Edited: 13/11/2009 2:34am

The template parser doesn't resolve variable arguments. That's not going to work.
But why creating such awkward constructs? Apparently you got the $StoryUrl available, so why not write a method on your controller like this:

public function StoryLink(){
return GetLinkFromID($this->StoryUrl);

Then in the template simply use:



Community Member, 904 Posts

13 November 2009 at 3:08am

Regarding the method provided by beeonline. I suggest you rewrite your method to something like this:

function GetLinkFromID($id = 1) {
	$do = DataObject::get_by_id('SiteTree', $id);
	if($do) return $do->Link();
	return '';

The DataObject::get_by_id method already allows you to get a DataObject by ID. Then use the Link method instead of the URLSegment. It will provide a correct link and will most likely also work with nested URLs. And I also introduced a check if you really get a DataObject.. just in case you pass an invalid ID to the function...

Go to Top