Get Any Field from the Parent of the Current Resource in MODX

In a previous article, I wrote about how to get the pagetitle of the current resource’s parent. It occurred to me that a more generic snippet that would get *any* field from the parent might be useful. Instead of the parent’s pagetitle, you might want its menuindex, longtitle, description, introtext (summary), or some other field.

This is a fairly minor modification of the previous snippet. The only real difference is that the name of the desired field is set as a snippet property and used as a variable in the snippet.

This tag goes where you want to display the parent’s field:

[[!getParentField? &field=`pagetitle`]]

And here’s the snippet:

/* getParentField snippet */

$parentObj = $modx->getObject('modResource', $modx->resource->get('parent'));
$field = $scriptProperties['field'];

if ($parentObj) {
    return $parentObj->get($field);
} else {
    return '';
}

MODX logo

See this page for a list of the available parent fields. You must use the exact field name (in all lowercase) for the field you want.

Here’s the more compact version:

/* getParentField snippet */

$parentObj = $modx->getObject('modResource', $modx->resource->get('parent'));
return $parentObj? $parentObj->get($scriptProperties['field']) : '';


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray

I am the author of MODX: The Official Guide and over 30 MODX add-on components. I host Bob's Guides, a source of valuable information for MODX users, and I've been very active in the MODX Forums with over 14,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *


8 × 5 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>