SQBiz
1
How can I use $site or $pages in a custom page method? For instance, a basic method to retrieve a field from the current page’s first-level parent:
page::$methods['section'] = function($page) {
$url = explode('/', $page->uri());
return kirby()->site()->pages()->find($url[0])->description();
};
This won’t work, kirby()
is not defined here. Should I just pass in $pages as an extra argument?
Your code above works for me, although kirby()->site()->($url[0])->description()
without the pages()
bit is enough?
From a pages perspective, you can address the kirby object like this as well:
$page->kirby()->site()-> ...
But that should not be necessary.
You can even use the page()
helper:
return page($url[0])->text();
Or use something like this:
page::$methods['section'] = function($page) {
if($page->parents()->count()) {
return $page->parents()->last()->title();
} else {
return $page->title();
}
};
SQBiz
3
Thanks, I didn’t know about ->last()
or the page helper!