
5th
Nov 09
Nov 09
MODx – Include Page Content Snippet
In MODx I added an “advertisement” page for a new project to allow people using the manager to change the adverts displayed throughout the site. I wanted to include that page in a similar fasion to PHPs include and require functions. I found a static method of doing it via the MODx “extras” repository so decided to extend it to allow a custom page ID to be used wherever.
To use the snippet login to the MODx manager, click the Elements Tab then Manage Elements. Go to the Snippets tab then click New Snippet. Call the snippet IncludePage, paste the code below then save.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php /* :::::::::::::::::::::::::::::::::::::::: Snippet name: IncudePage Short Desc: Include a MODx page content Version: 1.0 Author: Daniel Gibbs (danielgibbs.net) Date: 5th November 2009 :::::::::::::::::::::::::::::::::::::::: Example Usage: [[IncludePage? &pageID=`12`]] :::::::::::::::::::::::::::::::::::::::: */ # check if inside manager if ($m = $modx->insideManager()) { return ''; # don't go any further when inside manager } if (!isset ($pageID)) { return "<p>Error: No page ID specified, can't include content...</p>"; } else { $content = $modx->getTemplateVarOutput('content', $pageID); return $content['content']; } ?> |
Example Usage:
1 | [[IncludePage? &pageID=`12`]] |
This should be self explanatory but… setting pageID to 12 will include page 12. Page id’s are displayed in the document tree to the left of the manager next to the page title; i.e. Advertise (12).







Lance 19 December 2009 9:38 am
Hi there,
fantastic snippet, was looking for something just like this.
just one mention, in the comments of your script you name it “Snippet name: IncudePage”, it’s missing the “l”, this isn’t super important except if one is lazy and copy/pastes that as the snippet’s name and finds the snippet doesn’t work if they call “IncludePage”
Gibbs 21 December 2009 10:07 am
Heh
Thanks for pointing out the typo Lance.
eRage website 28 July 2010 3:55 pm
Hey, it’s a nice tip.. but isn’t that what the GetField snippet is for, or am I missing something here?
Jonathan 5 August 2010 1:43 pm
I cant get this thing working, where am I supposed to place the:
[[IncludePage? &pageID=`PAGE_ID_NUMBER`]] ?
Gibbs 13 August 2010 10:16 am
Hi Jonathan,
You can place it pretty much anywhere. Usually in a pages content or in a template though.