我是 ModX 新手。我有像这样相互嵌套的资源:
resource1
- resource1-child1
- resource1-child2
resource2
- resource2-child1
- resource2-child2
和这段代码:
[[!getResources?
&includeContent=`1`
&parents=`[[pdoField?
&id=`[[*id]]`
&field=`id`
&topLevel=`4`]]`
&resources=`-[[*id]]`
&includeTVs=`1`
&processTVs=`1`
&sortby=`menuindex`
&sortdir=`asc`
&depth=`10`
&limit=`100`
&tpl=`allDoctors`
&where=`{"template:=":104}`]]
但由于某种原因,它不按菜单索引对元素进行排序。看起来它根本不会对任何内容进行排序,除非我从父资源中提取子资源。我怎样才能让它按文件夹菜单索引然后子菜单索引对所有内容进行排序?预先感谢
我自己在这里找到了解决方案,通过创建一个名为 getResourcesTree 的代码片段
<?php
if (!function_exists('multiarray_keys')) {
function multiarray_keys($ar) {
foreach ($ar as $k => $v) {
$keys[] = $k;
if (is_array($ar[$k]))
$keys = array_merge($keys, multiarray_keys($ar[$k]));
}
return $keys;
}
}
$parents = (!empty($parents) || $parents === '0') ? explode(',', $parents) : array($modx->resource->get('id'));
$depth = isset($depth) ? (integer) $depth : 10;
$tree = $modx->getTree($parents, $depth);
$tree = multiarray_keys($tree);
$tree = implode(',', $tree);
$tree = 'FIELD(modResource.id, ' . $tree . ')';
return $tree;
整理如下:
[[!getResources?
&includeContent=`1`
&parents=`[[pdoField?
&id=`[[*id]]`
&field=`id`
&topLevel=`4`]]`
&resources=`-[[*id]]`
&includeTVs=`1`
&processTVs=`1`
&sortby=`[[!getResourcesTree?
&parents=`[[pdoField?
&id=`[[*id]]`
&field=`id`
&topLevel=`4`]]`
&depth=`100`]]`
&sortdir=`asc`
&depth=`10`
&limit=`100`
&tpl=`allDoctors`
&where=`{"template:=":104}`]]