
Nov 09
MODx – Include Page Content Snippet
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).
MODx Revolution
As pointed about by a viewer via email this doesn’t work in MODx Revolution. This is because getTemplateVarOutput has been deprecated in the latest MODx. The updated script below (be warned, not thoroughly tested) will work for Revolution.
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: IncludePage Short Desc: Include a MODx (Revolution) resource content Version: 0.1 Author: Daniel Gibbs (danielgibbs.net) Date: 20th Jan 2011 NOT TESTED - Should work though :::::::::::::::::::::::::::::::::::::::: 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><strong>Error: No page ID specified for [[IncludePage]]</strong></p>"; } else { $res = $modx->getObject('modResource', (int)$pageID); return ($res->get('content')); } |
Bookmark or share this page:
Related posts:
- Determing Login State (MODx)
- Changing MODx Managers Document Tree View Nodes
- PHP URL Rewriting Without Apache and mod_rewrite
- GIMP Graphical Content Divider
MSN Contact: contact [at] danielgibbs.net
Dan Gibbs is a web developer, designer and SEO consultant involved in devon web design.






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.
Gibbs 22 January 2011 6:30 pm
Updated for MODx Revolution. Still very basic but thanks go to Patrick for emailing that it didn’t work under Revolution.
http://sudo.co/code/modx/includepage-revolution.modx