Skip to content

Commit

Permalink
Added instructions to check whether element exists
Browse files Browse the repository at this point in the history
  • Loading branch information
lindseydiloreto committed Apr 21, 2019
1 parent b0c8c4e commit 549fd09
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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](https://craftcms.com/features/multi-site) feature to access multiple languages on your website._
_* Note: You must be using Craft's [multi-site](https://craftcms.com/features/all#multi-site) feature to access multiple languages on your website._

***

Expand Down Expand Up @@ -79,6 +79,8 @@ A simple piece of code like this one will work great across 99% of sites:

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:

```twig
Expand All @@ -91,6 +93,23 @@ If you want to dynamically loop through each of your sites, try this instead:
</ul>
```

### Checking whether a translated element exists

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

```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>
```

***

## Anything else?
Expand Down

0 comments on commit 549fd09

Please sign in to comment.