Ndugu
1
In a template I have:
<?= $page->children()->first()->url() ?>
In another template I need to get the second sub element.
<?= $page->children()->second()->url() ?>
Doesn’t do it, because the keyword “second” doesn’t exist, I guess.
It is meant as a link from to a listing page.
Use nth()
method like that:
<?= $page->children()->nth(1)->url() ?>
Note: the index starts at 0
Ndugu
3
Oh! Of course! Thank you very much!!
@Ndugu Please check if the page exists before calling the url()
method:
<?php
if ($child = $page->children()->nth(2)) {
echo $child->url();
}
Otherwise, you will run into an error if the page doesn’t exist.
Ndugu
5
So should I change the other one as well?
This
<?= $page->children()->first()->url() ?>
is for pagination, right? Or is it a shorthand? And why does it exist, when it could cause an error?
This gets the first child of all subpages of a page. But if the collection is empty (i.e. the page has no children), it will return an error.
It has nothing to do with pagination.