I defined my menu using a structured field.
I want to highlight the active link.
$items = $site->menu_items()->toStructure();
foreach ($items as $item): ?>
<a <?php e($item->isOpen(), ' class="active-link"') ?> href="<?= $site->url() ?><?= $item->item_link() ?>">
<?= $item->item_name() ?>
<?php endforeach ?>
I think the “isOpen” only works with “<?php foreach ($site->children()->listed() as $item): ?>” right?
How is it possible to highlight an active link when i use a structured field?
Thank you for yor help.
What do your menu items actually link to? Pages?
some items link to pages and some items are anchors.
Well, unfortunately you didn’t post your structure field, but for the actual page links, you can convert the field value to page with
toPage(). For this object you can then test if the item is open.
If the value is just a string, it really depend to which page they actually link. Is it just a URL in that case?
Otherwise, you would have to check the URL to determine which link is active.
My structure field:
Ok, since you are not storing pages but only text or urls, you have to check if the current urls is the same as your item link.