I have a rich text field with a handle of myTextField that I have included in a global set called firstGlobalSet.

What If I created another global set called secondGlobalSet and included the same field myTextField.

How could I get the correct field from the correct global set based on url segment?

For example, mysite.com/first or mysite.com/second


You will need to access a few things.

  • The url segment you need (for example the last segment)
  • The global set name
  • The field handle name

You can use getLastSegment to access the last url segment.

Using the global set names above let's say I have a url like this:


{% set segment = craft.request.getLastSegment() %}  // first

I know I've got a global set called firstGlobalSet, and now I've got first. We just need to put them together. You can use Craft's getSetByHandle method to access the global set via its handle.

{% set global = craft.globals.getSetByHandle(segment ~ 'GlobalSet') %}
{{ global }} // firstGlobalSet

Now that we've got the correct global set, we can get to the field we need. In this case it's myTextField.

{{ global.myTextField }}

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.