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:
[code][[!getParentField? &field=`pagetitle`]][/code]
And here’s the snippet:
[code language=”php”]
/* getParentField snippet */
$parentObj = $modx->getObject(‘modResource’, $modx->resource->get(‘parent’));
$field = $scriptProperties[‘field’];
if ($parentObj) {
return $parentObj->get($field);
} else {
return ”;
}
[/code]
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:
[code language=”php”]
/* getParentField snippet */
$parentObj = $modx->getObject(‘modResource’, $modx->resource->get(‘parent’));
return $parentObj? $parentObj->get($scriptProperties[‘field’]) : ”;
[/code]
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!