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.

Customising the CMS /

How to get the current page ID dynamically?


Reply


4 Posts   5812 Views

Avatar
theAlien

Community Member, 131 Posts

20 March 2009 at 4:32am

Hi,

How do I extract the current page ID (to $id)?
I would like to do something like this:

$contactform = new TreeDropdownField("ContactformID","Contactform","SiteTree");
$contactform->setTreeBaseID($id);
$contactform->setFilterFunction(create_function('$item','return $item->ClassName == "ContactForm";'));
$fields->addFieldToTab("Root.Content.Main",$contactform);

This way I would like to retrieve all contactforms that are children of the current page.
If I'm asking something that's impossible: please tell me. Otherwise: give me a clue ;-)

Avatar
Ben Gribaudo

Community Member, 181 Posts

21 March 2009 at 2:13am

You should be able to get the current page's ID with:

$this->ID;

Hope that helps,
Ben

Avatar
theAlien

Community Member, 131 Posts

21 March 2009 at 12:23pm

Shame on me... that's just too easy... I should've thought about it myself :$
I didn't, so thanks alot. It worked right away.

Avatar
merrick_sd

Community Member, 97 Posts

23 November 2010 at 12:29am

How do i get the current page id. it doesn seem t work when withing a function
I just seem to get the ID of FlashAdvertChoice table

function MyAdverts() {
$adverts = DataObject::get_one("MainFlashAdvert");
$CurrentPageID = $this->ID;
return ($adverts) ? DataObject::get("FlashAdvertChoice", "PageID = $CurrentPageID ", "ID DESC", "") : false;
}

.ss

control MyAdverts
$CurrentPageID

end_control