I’m trying to output a specific title inside a structure field from a specific child page.
I currently have 3 children pages, but I don’t want to output all 3 titles, just one specific title.
Menu template
<?php $childPages = page('Smaller/Starter Plates');
foreach($childPages as $p): ?>
<?php foreach ($p->headone()->toStructure() as $item): ?>
<p><?php echo $item->title()?></p>
<?php endforeach; ?>
<?php endforeach ?>
I’m trying to use the find option to find the specific child page. The title of the page is “Smaller/Starter Plates” so i’m assuming I’m referencing it correctly?
I am then trying to access my structure field to grab the titles.
Here is the blueprint
headone:
label: Item One
type: structure
entry: >
{{title}}<br />
€{{price}}
fields:
title:
label: Title
type: text
price:
label: Price
type: tex
Where am I going wrong? Thank you for looking!
$childPages is a single, specific page, you can’t loop through that. Only use the inner foreach loop.
<?php
$childPage = page('Smaller/Starter Plates');
foreach ($childPage->headone()->toStructure() as $item): ?>
<p><?php echo $item->title()?></p>
<?php endforeach ?>
1 Like
Getting an error using that piece of code above. Im starting to assume ive referenced the page incorrectly?
Try to avoid capital letters and especially spaces in your file names.
Your folders should be named:
/smaller
-- starter-plates
Then in your code
$childPage = page('smaller/starter-plates');
I assume your /smaller
page is on the first level of pages?
And what sort of error do you get? It helps to post that as well. Thank you:slight_smile:
1 Like
The actual name for the page is “Smaller/Starter Plates” , the next one is “Drinks” and etc…
They are all one level below my menu page and are all sub-pages of menu.
I tried substituting in drinks to check if it was the backslash or spaces but no luck.
Currently getting this error;
Fatal error: Uncaught Error: Call to a member function headone() on boolean in C:\Users\DazdLaptop\Desktop\work\sites#\app\site\templates\menu.php:25 Stack trace: #0 C:\Users\DazdLaptop\Desktop\work\sites#\app\kirby\toolkit\lib\tpl.php(22): require() #1 C:\Users\DazdLaptop\Desktop\work\sites#\app\kirby\kirby.php(705): Tpl::load(‘C:\Users\DazdLa…’) #2 C:\Users\DazdLaptop\Desktop\work\sites#\app\kirby\kirby.php(680): Kirby->template(Object(Page), Array) #3 C:\Users\DazdLaptop\Desktop\work\sites#\app\kirby\kirby.php(781): Kirby->render(Object(Page)) #4 C:\Users\DazdLaptop\Desktop\work\sites#\app\index.php(16): Kirby->launch() #5 {main} thrown in C:\Users\DazdLaptop\Desktop\work\sites#\app\site\templates\menu.php on line 25
I see, so it should be:
$childPage = page('menu/drinks');
As I said, don’t use slashes or spaces or capital letters in your file names, you will only run into problems.
You need to pass the complete path to the page to the page helper.
1 Like
I managed to get the smaller/starter plates to work by using <?php $childPage = page(‘menu/smaller-starter-plates’);
I just copied the url for the title inside the panel!
Thank you for all your help, it’s really appreciated!