PHP code example of doublesecretagency / craft-siteswitcher

1. Go to this page and download the library: Download doublesecretagency/craft-siteswitcher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

doublesecretagency / craft-siteswitcher example snippets

twig
{% set element = (category ?? entry ?? null) %}
twig
{% 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>
twig
{% 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>
twig
{% set element = (category ?? entry ?? null) %}

<ul>
    {% for site in craft.app.sites.getAllSites() %}
        {% set siteLink = siteSwitcher(site.handle, element) %}
        {% if siteLink %}
            <li><a href="{{ siteLink }}">{{ site.name }}</a></li>
        {% endif %}
    {% endfor %}
</ul>
twig
{% set element = (category ?? entry ?? null) %}

<ul>
    <li><a href="{{ siteSwitcher('english', element, true) }}">English</a></li>
    <li><a href="{{ siteSwitcher('spanish', element, true) }}">Español</a></li>
    <li><a href="{{ siteSwitcher('french', element, true) }}">Français</a></li>
    <li><a href="{{ siteSwitcher('german', element, true) }}">Deutsch</a></li>
</ul>