Site Switcher
This plugin provides an easy way to switch between sites on your website. Regardless of which page you are currently on, you will be linked to the same page in its parallel site.
The Twig Function
siteSwitcher(siteHandle, element = null)
siteHandle
- The handle of your site (ie:english
).element
- (optional) If the current page is anentry
(or another element type), you can pass that element in as the second parameter. This ensures that any translated slugs are properly used.
Returns a URL which links to the alternate-site version of the current page. If the siteHandle
or element
is invalid, false
will be returned instead.
How to Use
A simple piece of code like this one will work great across 99% of sites:
{% set element = (category ?? entry ?? null) %}
<ul>
<li><a href="{{ siteSwitcher('english', element) }}">English</a></li>
<li><a href="{{ siteSwitcher('spanish', element) }}">Español</a></li>
<li><a href="{{ siteSwitcher('french', element) }}">Français</a></li>
<li><a href="{{ siteSwitcher('german', element) }}">Deutsch</a></li>
</ul>
You can use this code in an include
, and share it across your entire website. If the page is an entry
page, it will use the localized version of that entry's slug. Otherwise, it will simply retain the same URI for each link.
If you want to dynamically loop through each of your sites, try this instead:
{% set element = (category ?? entry ?? null) %}
<ul>
{% for site in craft.app.sites.getAllSites() %}
<li><a href="{{ siteSwitcher(site.handle, element) }}">{{ site.name }}</a></li>
{% endfor %}
</ul>
To install this plugin, copy the command above to your terminal.
This plugin doesn't have any reviews.