bogus
April 3, 2023, 10:12pm
1
Hi all.
I tried to set up a structure field in order to display some links in my footer.
my blueprint looks like this
my_links:
label: My Links
type: structure
fields:
title:
label: Title
type: text
required: true
url:
label: URL
type: url
required: true
my corresponding template code is derived from the example in the structure field documentation
<?php $my_links = $page->my_links()->toStructure() ?>
<?php if ($my_links->count()): ?>
<ul>
<?php foreach ($my_links as $link): ?>
<li><a href="<?= $link->url() ?>"><?= $link->title() ?></a></li>
<?php endforeach ?>
</ul>
<?php else: ?>
<p>No links found.</p>
<?php endif ?>
I only get “no links found” although i put some up in my panel. It gets svaed like this:
My-links:
-
title: neeeew
url: https://example.com
I can’t find where I went wrong? Anybody can point me in the right direction?
bogus:
$page
In the footer, $page refers to the current page, don’t know where you have this field, but you need to refer to that particular page.
bogus
April 4, 2023, 7:54pm
3
that was what I thought it would do because the footer is (as snippet) part of that current page. now that i changed it to “site” it works. Thank you!
bogus
April 4, 2023, 8:41pm
4
but now as I’m advancing from the simpler example I’ve now added a toggle which lets me decide wheter I use an external link with url or i choose one of my own kirby pages to be linked to.
works in concept but now kirby throws a weird error from its guts when I simply want to link to a page. my template goes like this:
<ul>
<?php foreach ($site->footer_links()->toStructure() as $link): ?>
<?php if ($link->show_external_link()->toBool()): ?>
<li><a href="<?= $link->url()->html() ?>" target="_blank"><?= $link->text()->html() ?></a></li>
<?php else: ?>
<li><a href="<?= $link->page()->url() ?>"><?= $link->text()->html() ?></a></li>
<?php endif ?>
<?php endforeach ?>
</ul>
on this portion it breaks
<li><a href="<?= $link->page()->url() ?>"><?= $link->text()->html() ?></a></li>
$link->page() returns a field object which doesn’t have a url method. See docs how to render a page from a pages field: Pages | Kirby CMS