Hello,
I found this code which iterates all fields of the page. How could I make it to NOT include some fields?
<?php foreach( $page->content()->fields() as $f ): ?>
<?= $page->$f() ?> <br />
<?php endforeach ?>
Thanks.
Hello,
I found this code which iterates all fields of the page. How could I make it to NOT include some fields?
<?php foreach( $page->content()->fields() as $f ): ?>
<?= $page->$f() ?> <br />
<?php endforeach ?>
Thanks.
Like this:
$fields = $page->content()->fields();
unset($fields['text']);
foreach ($fields as $field) {
echo $field;
}
Note that it is enough to echo $field
you don’t have to call $page->{$field}()
again, since $field
is a field object.
Downside of this approach is that you don’t know the type of field and hence not the right field method to render its content (html()
, kt()
etc.):
This doesn’t seem to accept multiple fields. This gives me a syntax error.
unset($fields['title', 'price', 'description', 'cover', 'thumbnail']);
With regards to the fields I want to loop through, they are all type: text
.
This is just a matter of cleaning the code because I am using Tailwindcss and its a mess to style 30 lines of <li>
.
unset($fields['title'], $fields['price'], $fields['description'],$fields['cover'],$fields['thumbnail']);
Or get the keys from the $fields
array and then do an array_diff
.
Alright, this works but the uploaded images are being included.
This is my .yml
images:
label: Images
icon: image
sections:
images:
headline: Images
type: files
template: image
layout: cards
image:
cover: true
I tried to add $fields['images']
but it doesn’t work.
images
is not a field, it can’t possibly be in the content file and so you can’t unset()
it.
I don’t really understand the problem.
This is being echoed in the loop. And this is the original filename of the image. I have already removed the cover, thumbnail
fields which has different generated names.
Edit:
Wait, what do you mean by this?
I can’t possibly know where it comes from, maybe an image included in a text field?
Well, yeah it’s from the old blueprint . It’s indeed on the .txt file.
Thank you.
Here’s a little script if you want to clean up your text files:
Pardon my knowledge but how can I run this?
I tried putting it a new php file in the root folder. Opened up http://site.test/clean.php
but I think it’s not working. I’ve checked my edited text file and it wasn’t cleaned up.
If you want to run it from the root folder, you have to include the bootstrap file and define the $kirby
and $site
variables (require __DIR__ . '/kirby/bootstrap.php';
)
However, you can also put it into any template, eg. the home.php
template to make your life easier.
Don’t forget to make a backup first.