find('texts'); $years = $textsPage->children(); $texts = $years->children()->index(); $categories = array(); foreach ($textsPage->categories()->split() as $category) { $categories[$category] = array( 'title' => $category, 'texts' => array() ); } foreach ($texts as $text) { try { $categories[$text->category()->value()]['texts'][] = $text; } catch (\Throwable $th) { throw new Exception(json_encode($th->getFile() . ' : ' . $th->getMessage())); } } return $categories; };