承接 doublesecretagency/craft-siteswitcher 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

doublesecretagency/craft-siteswitcher

最新稳定版本:3.0.0

Composer 安装命令:

composer require doublesecretagency/craft-siteswitcher

包简介

Easily switch between sites on any page of your website.

README 文档

README

Easily switch between sites on any page of your website.

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.

* Note: You must be using Craft's multi-site feature to access multiple languages on your website.

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.

Basic Usage

Simply use a line of code like this:

<a href="{{ siteSwitcher('spanish') }}">Español</a>

That will link you to the Spanish version of the current page.

Advanced Usage

If the current page is an entry, your entry might be using different slugs for each language. In that case, you can pass the entry object as your second parameter:

<a href="{{ siteSwitcher('spanish', entry) }}">Español</a>

That second parameter can accept any Craft element type, so you can use other elements as well! Obviously, only elements which make use of slugs (like category elements) will work.

<a href="{{ siteSwitcher('spanish', category) }}">Español</a>

Sharing site links between entries & non-entries

Using the null-coalescing operator, you can chain potential element types. If one isn't defined on a particular page, the next value will be used.

{% set element = (category ?? entry ?? null) %}

This will use the first non-null value it finds (or null if no element exists).

In Conclusion

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.

Dynamically looping through all sites

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>

Checking whether a translated element exists

You can check to make sure that a translated version of that element exists before showing the link:

{% 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>

Fall back to the translated homepage

Assuming your homepage is properly translated across each site, you can set the homepage as a final backstop. If the link cannot otherwise be determined, it will link to the translated homepage instead.

Simply set the optional third parameter to true:

{% 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>

Anything else?

We've got other plugins too!

Check out the full catalog at doublesecretagency.com/plugins

统计信息

  • 总下载量: 85.64k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 66
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 66
  • Watchers: 1
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-30