In my previous blog post, 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 '';
}
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']) : '';

Comments (0)
Please login to comment.