1

I have an multisite installation like this:

  • www.example.com/animal
  • www.example.uk/animal

Each site has a configuration like so:

'example.com' => array(
        'siteUrl' => array(
            'us_en' => 'https://www.example.com/'
        )
'example.uk' => array(
        'siteUrl' => array(
            'uk_en' => 'https://www.example.uk/'
        ).

I want to print a collection of URLs from all of the locales. For example:

  • www.example.com/animal
  • www.example.uk/animal

And check their status as well.

2

I don't know what version of Craft you're using, but recently it became possible to request multiple site's version of the same entry/entries. For this to work, you need to provide multiple site ID's to the siteId part of the query, or multiple handles to the site part of the query:

{#
 # Provide the entry element query with the IDs for 3 sites
 # and group those by the entry's ID, so we know which entries belong together
 #}
{% set entriesById = craft.entries.siteId([1,2,3]).all() |group('id') %}

{#
 # Loop through the grouped entries and display whatever you need to display.
 # Each entry has their own `siteId` set, so you can use .getUrl() (or .url) to display the url
 #} 
{% for entryId, entries in entriesById %}
    <h2>Entry {{ entryId }}</h2>

    {% for entry in entries %}
        URL: {{ entry.url }} - Status: {{ entry.status }}  
    {% endfor %}
{% endfor %}

If you are using Craft 2 (or a Craft 3 version before it became possible to query multiple site's entry versions at once), I think you could do it like this:

{# Craft 2 (untested, it's been a while ... #}
{% for locale in craft.i18n.getSiteLocales() %}
    {% set entry = craft.entries.id([your entry's id]).locale(locale) %}

    {% if entry %}
        URL: {{ entry.url }} - Status: {{ entry.status }}
    {% endif %}
{% endfor %}

{# Craft 3, before my original answer became possible #}
{% for site in craft.app.sites.allSites %}
    {% set entry = craft.entries.id([your entry's ID]).siteId(site.id).one() %}

    {% if entry %}
        URL: {{ entry.url }} - Status: {{ entry.status }}
    {% endif %}
{% endfor %}
  • I'm currently using craft 2. – Nelson Nguyen Feb 17 at 8:25
  • Ah. I'll see if I can add a Craft 2 example to the answer :) – Paul Feb 17 at 8:26
  • Thank. You're saving me – Nelson Nguyen Feb 17 at 8:27
  • Unfortunately, getSiteLocales only show one site. – Nelson Nguyen Feb 17 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.