# Nav items in forLoop with Section Ids in multiple languages

**URL:** <https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879>\
**Category:** Questions\
**Tags:** v2\
**Created:** [July 5, 2017, 11:17am UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879 "2017-07-05T11:17:03Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![benfehrmanlee](https://avatars.discourse-cdn.com/v4/letter/b/c68b51/32.png) [@benfehrmanlee](https://forum.getkirby.com/u/benfehrmanlee)\
**Post date:** [July 5, 2017, 11:17am UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/1 "2017-07-05T11:17:03Z")

</div>

Hi, I’m new to working with one-pagers and multiple languages and have a question that I’m sure is relatively simple. I began with the Kirby boiler plate one-page structure. I need to create section ids corresponding to a navigation that pull from each $pages title, (this way allowing language toggling between Korean and English.

I have a nav anchored to assigned for each $page.

```
<ul class="nav">
	<li><a href="#about">About</a></li>
	<li><a href="#events">Events</a></li>
	<li><a href="#artists">Artists</a></li>
</ul>

```

Each section accordingly assigned an id.

```
<section id="about">
	<h1 class="make-block decompose-lines">
		<?php echo $data->text()->html() ?>
		<?php echo $data->year()->html() ?>
	</h1>
</section><!--END ABOUT-->

```

If I use the forLoop to call the $pages titles,

```
<ul class="nav">	
   <?php foreach($pages->visible() as $item): ?>
	<li><a href="<?php echo $item->url() ?>"><?php echo html($item->title()) ?></a></li>
   <?php endforeach ?>
</ul>

```

I’m not sure how to call the corresponding $pages title for each section id.

Thanks very much.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 5, 2017, 11:29am UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/2 "2017-07-05T11:29:18Z")

</div>

```auto
<ul class="nav">	
   <?php foreach($pages->visible() as $item): ?>
	<li><a href="<?= $site->url() . '/#' . str::slug($item->title()) ; ?>"><?= $item->title()->html() ?></a></li>
   <?php endforeach ?>
</ul>

```

Note that I use `str::slug()` here to make the title URL compatible. Please also note that this won’t work if you use hardcoded IDs in your sections, you would have to use the language specific IDs there as well.

---

<div class="post-metadata">

**Author:** ![benfehrmanlee](https://avatars.discourse-cdn.com/v4/letter/b/c68b51/32.png) [@benfehrmanlee](https://forum.getkirby.com/u/benfehrmanlee)\
**Post date:** [July 5, 2017, 11:49am UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/3 "2017-07-05T11:49:53Z")

</div>

Yes, with multiple languages, I’m trying to discern how to call the section IDs from the $page, opposed to hardcoded IDs (since I have both .kr.txt and .en.txt files for all $pages).

Thanks!

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 5, 2017, 12:01pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/4 "2017-07-05T12:01:06Z")

</div>

> [@benfehrmanlee](#):
>
> \<section id=“about”\>

In your sections, you would have to define the ID using the item title as well:

```auto
<section id="<?= str::slug($data->title()) ?>">

```

---

<div class="post-metadata">

**Author:** ![benfehrmanlee](https://avatars.discourse-cdn.com/v4/letter/b/c68b51/32.png) [@benfehrmanlee](https://forum.getkirby.com/u/benfehrmanlee)\
**Post date:** [July 7, 2017, 10:54am UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/5 "2017-07-07T10:54:18Z")

</div>

The site is set to default to Korean in the config.php

```
c::set('languages', array(
  array(
    'code' => 'en',
    'name' => 'English',
    'locale' => 'en_US',
    'url' => '/en',
  ),
  array(
    'code' => 'kr',
    'name' => '한국어',
		'default' => true,
    'locale' => 'kr_KR',
    'url' => '/kr',
  ),
));

```

So, with the addition of the language specific url, would the string read like this?

```
<ul class="nav">	
   <?php foreach($pages->visible() as $item): ?>
	<li><a href="<?= $site->url() . '/lang' . '#' . str::slug($item->title()) ; ?>"><?= $item->title()->html() ?></a></li>
   <?php endforeach ?>
</ul>
```

---

<div class="post-metadata">

**Author:** ![benfehrmanlee](https://avatars.discourse-cdn.com/v4/letter/b/c68b51/32.png) [@benfehrmanlee](https://forum.getkirby.com/u/benfehrmanlee)\
**Post date:** [July 8, 2017, 10:49am UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/6 "2017-07-08T10:49:00Z")

</div>

Unfortunately this solution doesn’t seem to work. The nav items appear in the proper language, but the links do not work. The URL returns empty for all the nav items.

Is it something to do with how my languages are configured?

```
c::set('lang.detect', true);
c::set('lang.support', true);
c::set('lang.default', 'kr');
c::set('lang.available', array('en', 'kr'));

c::set('languages', array(
  array(
    'code' => 'kr',
    'name' => '한국어',
	'default' => true,
    'locale' => 'kr_KR',
    'url' => '/kr',
  ),
  array(
    'code' => 'en',
    'name' => 'English',
    'locale' => 'en_US',
    'url' => '/en',
  ),
));

```

It seems that if the nav is displaying properly for each language as an $item, then its a problem with the way the is linked. But it just seems like the item does not link properly:

`<section id="<?= str::slug($data->title()) ?>">`

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 8, 2017, 1:12pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/7 "2017-07-08T13:12:37Z")

</div>

Please remove these options:

```auto
c::set('lang.support', true);
c::set('lang.default', 'kr');
c::set('lang.available', array('en', 'kr'));

```

They don’t exist.

Also, in your link you are using a `lang` string, that does not make sense.

In your navigation, do this instead:

```auto
<ul class="nav">	
   <?php foreach($pages->visible() as $item): ?>
	<li><a href="<?= $site->language()->url() . '/#' . str::slug($item->title()) ; ?>"><?= $item->title()->html() ?></a></li>
   <?php endforeach ?>
</ul>

```

---

<div class="post-metadata">

**Author:** ![benfehrmanlee](https://avatars.discourse-cdn.com/v4/letter/b/c68b51/32.png) [@benfehrmanlee](https://forum.getkirby.com/u/benfehrmanlee)\
**Post date:** [July 8, 2017, 1:19pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/8 "2017-07-08T13:19:22Z")

</div>

Thank you for your ongoing help with this, and sorry for my confusion.

Would the section IDs then also need the language specific item title as well?  
Because this still does not seem to be working.

`<section id="<?= str::slug($data->title()) ?>">`

---

<div class="post-metadata">

**Author:** ![benfehrmanlee](https://avatars.discourse-cdn.com/v4/letter/b/c68b51/32.png) [@benfehrmanlee](https://forum.getkirby.com/u/benfehrmanlee)\
**Post date:** [July 8, 2017, 1:23pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/9 "2017-07-08T13:23:29Z")

</div>

In other words, clicking each of the three nav item link only yields this in the url:

`localhost:8888/kr#`

And when I switch to the /en site, clicking the nav links take me to the default /kr.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 8, 2017, 3:42pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/10 "2017-07-08T15:42:40Z")

</div>

That’s a bit weird, apart from the title, there’s also the slash missing. Are you sure you pasted the code exactly as above? (note that there was a `>` before `url()`, I have corrected it above)

---

<div class="post-metadata">

**Author:** ![benfehrmanlee](https://avatars.discourse-cdn.com/v4/letter/b/c68b51/32.png) [@benfehrmanlee](https://forum.getkirby.com/u/benfehrmanlee)\
**Post date:** [July 8, 2017, 4:01pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/11 "2017-07-08T16:01:20Z")

</div>

The English is now working properly, linked to the nav items!

Stranger still, the Korean links are still all yielding an empty url as before.

`localhost:8888/kr/#`

Every $page has a .en.txt and .kr.txt file, with corresponding titles. Can you think of anything at all I might be overlooking?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 8, 2017, 4:08pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/12 "2017-07-08T16:08:54Z")

</div>

I did a quick test with an arbitrary Korean string, and looks like `str::slug()` does not work with Korean…

---

<div class="post-metadata">

**Author:** ![benfehrmanlee](https://avatars.discourse-cdn.com/v4/letter/b/c68b51/32.png) [@benfehrmanlee](https://forum.getkirby.com/u/benfehrmanlee)\
**Post date:** [July 8, 2017, 4:16pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/13 "2017-07-08T16:16:30Z")

</div>

Ah, I see. Could I include a Site-\>Languages folder with a kr.php file and just run some site-wide translations for those three nav items?

```
<?php

l::set('about', '소개');
l::set('events', '이벤트');
l::set('artists', '아티스트');

?>

```

And then call them when needed in the snippet?

`<?php echo l::get('events') ?>`

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 8, 2017, 5:52pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/14 "2017-07-08T17:52:16Z")

</div>

If you are just using one word titles, you might as well remove the `str::slug()` part.

---

<div class="post-metadata">

**Author:** ![benfehrmanlee](https://avatars.discourse-cdn.com/v4/letter/b/c68b51/32.png) [@benfehrmanlee](https://forum.getkirby.com/u/benfehrmanlee)\
**Post date:** [July 9, 2017, 12:11pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/15 "2017-07-09T12:11:30Z")

</div>

I could remove the `str::slug()` from both the nav ul and the corresponding sectiond ids?

```
<ul class="nav">	
			   <?php foreach($pages->visible() as $item): ?>
				<li><a href="<?= $site->language()->url() . '/#' . $item->title() ; ?>"><?= $item->title()->html() ?></a></li>
			   <?php endforeach ?>
</ul>

```

And the section ids would just read:  
`<section id="<?php echo $data->title() ?>">` ?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 9, 2017, 12:13pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/16 "2017-07-09T12:13:50Z")

</div>

Yes, but as I said, **only** if you use one-word titles and only if your titles only contain characters that are allowed in IDs (HTML5’s only restriction is spaces, though), otherwise you could end up with IDs like `about us`, and that’s not possible.

---

<div class="post-metadata">

**Author:** ![benfehrmanlee](https://avatars.discourse-cdn.com/v4/letter/b/c68b51/32.png) [@benfehrmanlee](https://forum.getkirby.com/u/benfehrmanlee)\
**Post date:** [July 9, 2017, 1:28pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/17 "2017-07-09T13:28:13Z")

</div>

Yes of course, I understand. Everything is populating correctly and linked up, both for Korean and English.  
Thanks so much for your patience and help with this.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 9, 2017, 1:34pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/18 "2017-07-09T13:34:32Z")

</div>

You are welcome 🙂

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:35pm UTC](https://forum.getkirby.com/t/nav-items-in-forloop-with-section-ids-in-multiple-languages/7879/19 "2025-02-06T17:35:23Z")

</div>


