Jump to content

Open a page in the current language


pwired
 Share

Recommended Posts

post-1069-0-27858500-1450778938_thumb.jp

Hi,
I am working on a main navigation menu (start, faq, contact) and a sub navigation menu (para ipad, blackbox, mando a distancia) See screenshot above

When I select a language in the top right corner all menu items appear in their language correctly. So far so good.
Now, when I then click on a menu item, it has to open a page in the current language.

When I use this for example it will not open the contact page in the current language but in the default language (english) and also all menu items revert back to the default language (english)

<li id="contact"><a href="/contact/" title=""><?php echo $contact ?></a></li>

After some puzzeling I came up with this and now it opens the contact page in the current language and the menu items no longer revert back to the default language. So far so good.

echo "<li>" . "<a href=" . '"' . $pages->get('/contact/')->url . '"' . ">"  . $contact . "</a>" . "</li>";

It works, but when I look at that line of code it looks a bit over the top so I am wondering if there isn't a better or compacter way of doing this.

Link to comment
Share on other sites

Correct me if I understood wrong. This looks like you're assembling the navigation menus manually. Hardcoding an absolute path for a navigation entry will of course point to the page in its default language if you don't specify a prefix for a different language. In that case, fetching the page in question and assembling the link from its properties is the way to go. Perhaps you could add a function that abstracts away the repeated verbosity.

<?= navItem('/contact/') ?> <?= navItem('/faq/') ?>

and further down (or in _init.php or such) add your function:

function navItem($path) {
    $pg = wire('pages')->get($path);
    return "<li><a href='" . $pg->url . "'>" . $pg->title . "</a></li>";
}
  • Like 1
Link to comment
Share on other sites

This is the first time I had to do both a multi language navigation banner plus a sub navigation and somehow the sub navigation part got me tricked.

You guys are good at this:

echo "<li class='current'><a href='$child->url'>$child->title</a></li>";

that easy

Since I am really into delayed output lately (prepend _init.php append _main.php) I also like the function navItem approach

Thanks for your replies, I am back on track.

Link to comment
Share on other sites

What is shown in the link https://github.com/ryancramerdesign/ProcessWire/blob/dev/site-beginner/templates/_head.php#L28

is a for each loop that outputs all children and use the title as a visible word to click on.

But this is not always wanted as in my case.

I want some children (not all of them) as links in a sub navigation menu and I don't want to use the titles

of them as the visible words to click on, but other relevant word combinations.

I would not know how to change this for each loop in such a way that it outputs or filters out only the children that I wanted and the words that I wanted to click on. Hence the solution I came up with.

Maybe hanna code could make it translate and look more compact.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...