How to get the title from a text document of the second language


#1

I have a multi-language site with sections (like a one-pager), built from seperate text documents. I link to each section with an anchor. The url to the anchor is built from the page url + the title of the page.

For example i have these two text documents: a) page1_german.de.txt with “Title: page1_german” and b) page1_english.en.txt with “Title: page1_english”.

Now i need to link to the url+anchor of the translated site.

So far i have this code for the main.php and the section.php:

main.php:
<?php snippet('header'); ?>
<?php foreach(page('main')->children()->visible() as $section): ?>
<?php snippet($section->uid(), array('data' => $section)) ?>
<!— html for each content section —>
<?php endforeach ?>

section.php:
<?php foreach($site->languages() as $language): ?>
<a href="<?= $page->url($language->code()) ?>#<?php echo str::slug($data->title()) ?>"> <?= str::upper($language->code()) ?> </a>
<?php endforeach ?>

Part of it is from the ‘Switch B Example’ in the docs (https://getkirby.com/docs/languages/switching-languages):

But the following will only show the title (anchor) of the current language:
<?php echo str::slug($data->title()) ?>

How can i get the title of the translated text document?


#2

The content() method is your friend, because it accepts a language parameter: $data->content($language->code())->title().


#3

Perfect! Thanks a lot!