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 an entry (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>
Installation Instructions

To install this plugin, copy the command above to your terminal.

Active Installs
3,534
Version
2.3.0
License
MIT
Compatibility
Craft 3, Craft 4, Craft 5
Last release
March 29, 2022
Activity (30 days)
0
Closed Issues
1
Open Issues
0
Merged PRs
0
Open PRs