toStructure() is not working

Hi, I’m new at Kirby and try to get data from a structure field. If I do:


I get the value as string. But I need a structured array or object. But this does not work:


Is there another way?

First, you define your posts:

$posts = $mypage->post()->toStructure();

Then you can loop through your data:

foreach ($posts as $post): ?>
    <h2><?= $post->title() ?></h2> // given your structure field has a field called "title"
<?php endforeach ?>

See this for more information (“How to use in templates/snippets”):

Thx @Fluxium I already read the docs and did the code you send. The problem is, that $posts is not a array or object. $mypage->post()->toStructure() returns a string. Do you know how this could be?

This is how the posts will be read:

$myPage = page('mainsites')->children()->find("site32");
foreach($myPage->posts()->toStructure() as $post){
  echo $post->headline();

But post is a string here?

What is post()? Assuming that this is the structure field?

Yes it is a structured field. I try to write simple code instead of posting my whole code. Post should be a structured field. It is set in the yaml file. And if i do ->value() i get the whole values from the field… but as string. And also toStructure() returns a string.

toStructure() should return a structure object if you



Kirby\Cms\Structure Object
    [0] => 0
    [1] => 1
    [2] => 2

Ok. Yes… thx @texnixe you are right. This gives me back an object with:

Field Object
    [page] => mainsites/site-32
    [key] => posts
    [value] => - headline: Hello1
infotext: Hello world 1 im a text
- headline: Hello2
infotext: Hello world 2 im a text
- headline: Hello3
infotext: Hello world 3 im a text

Is there a way to get these fields structured? This is what I want to to. i need an array or object where I have

 ['headline' => 'headline1', 'infotext' => 'Hello World 1'],
['headline' => 'headline2', 'infotext' => 'Hello World 2'],
['headline' => 'headline3', 'infotext' => 'Hello World 3'],

Sorry… but this is something i did not found and I hope I dont have to write an own script for that.

Sorry. I just saw that dump($myPage->posts()); also gives the object…

This should be completely fine. If it doesn’t work, then there must be something else interfering.

1 Like

thx @texnixe … Now it works. I create an export for data in a new file and I had to add something to my file at the top:

require '../kirby/bootstrap.php';

I only had:

require '../kirby/bootstrap.php';