besides everything, that line in your template doesn’t look right. It should be:
$items = page('aphorismen')->aphorismen()->toStructure();
besides everything, that line in your template doesn’t look right. It should be:
$items = page('aphorismen')->aphorismen()->toStructure();
Is there a aphorismen.txt
file in the content/2_aphorismen
folder?
Is it the only .txt
file in that folder?
So
page('aphorismen')
should indeed give you the right page object.
Therefore
dump(page(‘aphorismen’)->aphorismen()->toStructure());
Should output the structure collection and you should be able to filter this collection. Make sure that the fieldname is correct and the field actually has content.
$items = page('aphorismen')->aphorismen()->toStructure()->filterBy('toggle', true);
This is working!
I had been using =>
instead of this ->
what is the difference?
Now I need to bring in more pages, how is that achieved, by adding the names of the pages?
Is use in arrays to assign key/value pairs:
$array = [
'name' => 'Jane',
'job' => 'Manager',
];
Also used for arrow functions: PHP: Arrow Functions - Manual
Is called the “object operator”, you use it to call methods on objects $page->url()
, where $page is an object of the Kirby\Cms\Page class and url()
a member method of that class.
Thank you @texnixe.
Do you have any tips to how I can add more pages, in total I need to bring data from five pages.
Add more page to what?
Do you mean create pages, manually, programmatically or via the Panel?
Or add pages to a pages collection?
Here I am only looking at the ‘aphorismen’ page I need to look at four other pages too
And do these other pages also have structure fields whose items you want to add to the $items
collection?
yes they do.
You could try to append them with add()
,
$aphorismen = page('aphorismen')->aphorismen()->toStructure()->filterBy('toggle', true);
$itemsFromPageX = page('otherpage')->aphorismen()->toStructure()->filterBy('toggle', true);
$aphorismen = $aphorismen->add($itemsFromPageX);
Not 100% sure this works, but try.
Why is this added to the code?
Didn’t you say you had this structure field on several pages?
I don’t think it will work with structure fields that have a different structure or different field names. If the structure is the same and only the field name is different, then of course change the field name to the correct field name.
ok, this is pointing me towards the feeling that using the structure field type is not the best solution. Still I will try and see what occurs
@texnixe it is working! Thank you!
Now I need to sort by date and create if functions to show with the correct CSS depending on from which page the item is coming from…
And now I can also sort with date.
Stuck with the using ‘if and exists’ to show the correct presentation of the data.
This is what are am trying:
<?php if($item->quote()->exists()) ?>
<div class="blockquote-container odd-one">
<blockquote class="bq-style">
<p><?= $item->quote()->html() ?></p>
<footer>name</footer>
<h3 class="pb-3"><?= $item->titel()->html() ?></h3>
<?= $item->text()->kt() ?>
</blockquote>
</div>
<div class="container text-center my-2">
<div>
<p>Heutzutage, <?= $item->datum()->toDate('d. M. YYYY') ?></p>
</div>
</div>
<hr class="my-5 text-primary">
<?php endif ?>
I am checking to see if the field ‘quote’ exists and if so use following html.
I have two further ways to present the data, which I am hoping this would work so:
<?php if($item->quote()->exists()) ?>
the html and kirby stuff to be used
<?php endif ?>
<?php if($item->image()->exists()) ?>
the html and kirby stuff to be used
<?php endif ?>
<?php if($item->text()->exists()) ?>
the html and kirby stuff to be used
<?php endif ?>
So ‘quote’, ‘image’ & ‘text’ are field names. But to be honest I am confused.
What exactly is your problem? I think it would be better to check if the fields are not empty, rather than whether they exist.
Thanks @texnixe for the tip with ‘isNotEmpty’
I have used that method and I am happy to say all is working as planned
<?php
// using the `toStructure()` method, we create a structure collection
$items = page('aphorismen')->aphorismen()->toStructure()->filterBy('toggle', true);
$itemsFromLyrik = page('lyrik')->lyrik()->toStructure()->filterBy('toggle', true);
$itemsFromAntworten = page('antworten')->antworten()->toStructure()->filterBy('toggle', true);
$itemsFromCartoons = page('cartoons')->cartoons()->toStructure()->filterBy('toggle', true);
$itemsFromCartoonsinn = page('cartoonsinn')->cartoons()->toStructure()->filterBy('toggle', true);
$items = $items->add($itemsFromLyrik);
$items = $items->add($itemsFromAntworten);
$items = $items->add($itemsFromCartoons);
$items = $items->add($itemsFromCartoonsinn);
// Sort by date
$items = $items->sortBy(function ($page) {
return $page->datum()->toDate();
}, 'desc');
// we can then loop through the entries and render the individual fields
foreach ($items as $item): ?>
<?php if ($item->quote()->isNotEmpty()): ?>
<div class="blockquote-container odd-one">
<blockquote class="bq-style">
<p><?= $item->quote()->html() ?></p>
<footer>Thomas Bäder</footer>
<h3 class="pb-3"><?= $item->titel()->html() ?></h3>
<?= $item->text()->kt() ?>
</blockquote>
</div>
<div class="container text-center my-2">
<div>
<p>Heutzutage, <?= $item->datum()->toDate('d. M. YYYY') ?></p>
</div>
</div>
<hr class="my-5 text-primary">
<?php endif ?>
<?php if ($item->image()->isNotEmpty()): ?>
<div class="container text-center">
<?php foreach ($item->image()->toFiles() as $image): ?>
<img class="img-fluid" src="<?= $image->url() ?>">
<?php endforeach ?>
</div>
<div class="container text-center my-2">
<div>
<p>Heutzutage, <?= $item->datum()->toDate('d. M. YYYY') ?></p>
</div>
</div>
<hr class="my-5 text-primary">
<?php endif ?>
<?php if ($item->text()->isNotEmpty()): ?>
<figure class="text-center odd-five">
<blockquote class="blockquote">
<h3 class="pb-3"><?= $item->titel()->html() ?></h3>
<?= $item->text()->kt() ?>
</blockquote>
<figcaption class="blockquote-footer pt-3">
<cite title="Source Title">Thomas Bäder</cite>
</figcaption>
</figure>
<div class="container text-center my-2">
<div>
<p>Heutzutage, <?= $item->datum()->toDate('d. M. YYYY') ?></p>
</div>
</div>
<hr class="my-5 text-primary">
<?php endif ?>
<?php endforeach ?>
That is what I have used, should anybody see any mistakes please reply