0

I'm working on a multisite installation on Craft, and i want to know if it's possible to select a specific site to show for example a link to it, sitename and other attributes.

I couldn't find a way, maybe it's close to craft.app.sites but don't know how to do it, of course sites have his own handle. Thank you.

1 Answer 1

0

You're close, craft.app.sites in Twig gives you access to the service craft\services\Sites, which can be used to access specific sites. For example:

{# Get the primary site #}
{% set primarySite = craft.app.sites.getPrimarySite() %}

{# Get a specific site by handle #}
{% set myAwesomeSite = craft.app.sites.getSiteByHandle('myAwesomeSite') %}

{# Get an array with all sites #}
{% set allSites = craft.app.sites.getAllSites() %}

Use the class reference to find out what methods are available on the service.


I couldn't find a way, maybe it's close to craft.app.sites but don't know how to do it, of course sites have his own handle. Thank you.

A useful tip is to use the {% dd %} tag to find out what a variable contains:

{% dd craft.app.sites %}

Put this anywhere in your Twig template and it will tell you which class the variable is an instance of. Then you can look that up in the class reference and take it from there.

2
  • 1
    Amazing! Great explanation, thank you Moritz!
    – Sebastian
    Jul 29 at 12:56
  • @Sebastian Glad I could help! If your issue is solved, would you mind marking my answer as accepted? Thanks.
    – MoritzLost
    Jul 29 at 13: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.