Anchors for content items

I am looking for a way to add anchors to items in a content sections. The site.txt is structured like this:

Title: Descriptions of things
Text: Text introducing what the site is about
    item: Item name
    description: all about the item
    item: Item name 2
        description: all about the item
    item: Item name 3
    description: all about the item

and the site.php echoes all the descriptions in one go like this:

<?php echo kirbytext( $site[ 'description' ] ); ?>

The items can be chosen from a side menu, but no mater which one is chosen, the url of the page stays the same:
Is there a way to achieve etc without echoing the individual items separately?

Thank you in advance!!

I don’t understand your code. If you want to echo the content of the description items, you have to go through the collection in a loop, anyway:

$items = $site->descriptions()->toStructure();
foreach ($items as $item): ?>
//give the section an id with item name
<section id="<?php echo $item->item()">
//the rest of your code
<?php endforeach ?>

Then you can address each item individually in your sidebar menu.

Yes, sorry, I didn’t include all relevant code

<?php foreach ($descriptions as $index => $description): ?>
  <li class="c-toggleBoard--content <?php echo $index === 0 ? '' : 'is-hidden'; ?>" >
     <div class="row">
        <div class="column">
           <?php echo kirbytext( $description[ 'item' ] ); ?>
<?php endforeach ?>

So would you recommend setting the id in the loop?

It probably depends on how you want to address your containers. If you want to use Javascript, you might as well use a data attribute instead of an id.

1 Like