Say I’m building a submenu, where each item is a kirby-module, how would I access the title field of each module?
So far I can use modules()
, modulesList()
and other helpers, but the best I reached was to get to the module object and output it as a string unfortunately.
My code is
<?php $sections = $pages->filterBy('template', 'section'); ?>
<?php foreach($sections as $sec): ?>
<?= $sec->?? ?>
<?php endforeach ?>
I tried to get to the module page using ->grandChildren
but without good results.
The example of printing single modules worked, but I need to only access a specific field for all pages meeting the criterias above.
Thanks,
af
texnixe
December 28, 2017, 12:20pm
58
I don’t quite understand what you are trying to fetch here. All modules of all main pages? Could you post an outline of your structure?
A module is a standard page. So if we assume that a project page in a Starterkit had some module grandchildren:
- /project-a
project.txt
/modules
modules.txt
/1-section-gallery
module-gallery.txt
/2-section-text
module- text.txt
The you would get the modules like this:
<?php
$sections = $page->grandChildren()->visible();
foreach($sections as $section):
echo $section->title();
endforeach;
?>
Thank you!
Using ->grandChildren()
was giving me null
, so I’m using ->children()
twice like so
<?php $sections = $pages->filterBy('template', 'section')->children() ?>
<?php foreach($sections->children() as $sec): ?>
<?= $sec->title() ?>
<?php endforeach ?>
lukasbestle
Split this topic
January 11, 2018, 6:40pm
60
texnixe
Split this topic
July 7, 2020, 11:12am
61