i’ve used the code below to get exactly the info i want
<?php echo page('events')->children(); ?>
it outputs this list
events/event-a
events/event-b
events/event-c
Perfect! Now this is definitely a noob question but how can I add to this code to just output the first, second, third, etc. in the list.
<?php $p = page('events')->children()->nth(3); ?>
If you want to get more than one specific element, this syntax is not that useful though. I can tell you more if you write what exactly you want to get from the children collection.
Thanks you for your help! Really that simply huh. When i searched the children() function it didn’t even mention the nth() function. I don’t really need any deeper info. I’m trying to inject the ‘events/event-a’ collected from
<?php echo page('events')->children()->nth(0); ?>
into the code below where ‘events/event-a’ is hardcoded
<li class="event-list-item">
<?php $calendar1 = page('events/event-a')->calendar()->yaml(); ?>
<?php foreach($calendar1 as $calendarEntry) { ?>
<span class="event-date">
<span class="date">
<?php
$eventday=strtotime($calendarEntry['_begin_date']);
echo date('j',$eventday);
?>
</span>
Unfortunately, i can’t seem to simply store that info in a variable and echo it out into page(’’). What is the proper practice for getting the ‘events/event-a’ from the code above into the code below? Thanks again. Much appreciated!
That would be:
<?php $calendar1 = page('events')->children()->nth(0)->calendar()->yaml(); ?>
Or simpler in this case:
<?php $calendar1 = page('events')->children()->first()->calendar()->yaml(); ?>
The children()
method simply returns a Pages
object. It has a lot of methods you can use.
Awesome. I really need to improve my php game before my next project. Now if i wanted to dig deeper into that child i would just do something like this
<?php $calendar = page('events')->children()->nth(0)->calendar()->yaml();
foreach($calendar as $calendarEntry) {
echo $calendarEntry['eventname'];
}?>
It’s starting to sink in.